1 package main
2
3 import "fmt"
4
5 const p string = "Death & Taxes"
6 var q int = 42
7 const r = 98
8
9 // Compile errors
10 // --------------
11 //const q := 42
12 // q := 42
13
14 func main() {
15 fmt.Println("Printing const p - package level scope ", p)
16 fmt.Println("Printing q - package level scope ", q)
17 fmt.Println("Printing const r - package level scope ", r)
18 }
Uncommenting line #11, the error is -
$ go build 2.go
command-line-arguments
.\2.go:11: missing value in const declaration
.\2.go:11: syntax error: unexpected := after top level declaration
Uncommenting line #12, the error is -
$ go build 2.go
command-line-arguments
.\2.go:12: syntax error: non-declaration statement outside function body
Does the above error mean that ‘:=’ can only be used inside a function body to assign values to variables ?