var vs := in Go

后端 未结 3 900
滥情空心
滥情空心 2020-12-30 19:53

In the Go web server example here: http://golang.org/doc/effective_go.html#web_server

The following line of code works

var addr = flag.String(\"addr\         


        
3条回答
  •  既然无缘
    2020-12-30 20:23

    In Go, top-level variable assignments must be prefixed with the var keyword. Omitting the var keyword is only allowed within blocks.

    package main
    
    var toplevel = "Hello world"         // var keyword is required
    
    func F() {
            withinBlock := "Hello world" // var keyword is not required
    }
    

提交回复
热议问题