Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
package main
import "fmt"
func main() {
x := 5
fmt.Println("hi")
}It does not compile
- Expecting
hi: Go has no warnings, and a local variable that is declared but never read stops the build withdeclared and not used: x. - Expecting the problem to show up at run time: an unused variable is found by the compiler, so no program is built and nothing runs.
- How would you change the program so that it builds without using
xanywhere? - What does Go do with an imported package that the code never uses?