I think this part of the spec explains this behavior as an "implementation 
restriction": https://golang.org/ref/spec#Variable_declarations

Implementation restriction: A compiler may make it illegal to declare a 
> variable inside a function body 
> <https://golang.org/ref/spec#Function_declarations> if the variable is 
> never used.


On Saturday, December 30, 2017 at 9:51:39 AM UTC-7, leaf...@gmail.com wrote:
>
> Hi all, suddenly I found the code
> package main
>
> import (
>     "fmt"
> )
>
> var x=3
>
> func main() {
>     fmt.Println("Hello, playground")
> }
>
>
> It works fun, which surprised me. I mean, I get the idea that public 
> global variable can be exported so it will not trigger "the declared but 
> not used" error, but here x is a private variable: It cannot be used by 
> other packages and it is not used anywhere, which to me seems to be a 
> perfect match for an error. Did I miss something?
> Thanks in advance.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to