I\'ve been having a hard time trying to execute a simple golang program in a virtual machine powered by vagrant.
These are the relevant fields of my go env
:
In my case i was solve the problem by Using "VS Code" instead of default "text editor"
The problem was some extra characters present in the file. Once we remove extra characters it will work.
I wish it will solve to you also.
As said by already suggested by Nico, When you create a new project and new main.go file this error will appear when the file is not saved. Save the file (ctrl + s) and this error will disappear in both mac & windows. I faced the same issue and just got it resolved by doing ctrl+S on the main.go file.
As a new go user I came upon this answer looking for someone to tell me that I need to start my scripts with package main
although my error was a little different,
... expected 'package', found 'import'
It's real obvious now, but hey, that's how it goes.
Using VS Code for GO, and faced the same issue. Saving the file 'Ctrl+S' on Windows fixed the issue.
Reference : Answered by Nico
Just save the file first and than run the cammand.it is working.
go run main.go
For me, this also happened using Atom + Go Plus + Terminal Plus. The problem was that leading bracket was not on the "correct" line.
NOTE: Go Plus warns about syntax upon save, but I had imported this file after creating it locally with VIM, so I was never presented with the lint errors...
Error:
package main
import "fmt"
func main()
{
fmt.Println("hello world")
}
Correct:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}