69

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:

GOARCH="amd64"
GOPATH="/usr/local/src/go"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"

This is the program I'm trying to execute ( located in /usr/local/src/go/program ):

package program

import (
    "fmt"
)

func main() {
    fmt.Print("Aloha")
}

This, the output that I get:

main.go:4:5:
/usr/local/go/src/fmt/doc.go:1:1: expected 'package', found 'EOF'
package runtime:
/usr/local/go/src/runtime/alg.go:1:1: expected 'package', found 'EOF'

Take into account that this is a completely fake program. The weird thing is that it totally works in a different environment. What am I missing here?

Thanks a lot!

ThisIsErico
  • 1,885
  • 2
  • 19
  • 24
  • 49
    Did you saved your program.go source file before calling go run? And wouldn't it work better with `package main`? – VonC Jun 29 '15 at 07:44
  • Thanks for the reply! Yes, `package main` would be more appropriate. That's how it used to be and was failing with the exact same error. Sure, the file was saved ;) – ThisIsErico Jun 29 '15 at 08:40
  • Is there some kind of eol error (windows end of line instead of unix?) – VonC Jun 29 '15 at 08:43
  • Not really either :( – ThisIsErico Jun 29 '15 at 08:47
  • Interesting... Of course I'm getting an `EOF`... The files are indeed empty. The `go get` execution seems to be failing at some point. – ThisIsErico Jun 29 '15 at 08:54
  • Do not set GOROOT. GOROOT is cargo cult from the old days. – Volker Jun 29 '15 at 09:38
  • True thing, I was only setting `GOPATH`. `GOROOT` gets set automatically to the installation directory ( maybe it's the puppet module I'm using the one setting it ). – ThisIsErico Jun 29 '15 at 09:59

15 Answers15

148

Using VS Code for GO, and faced the same issue. Saving the file 'Ctrl+S' on Windows fixed the issue.

Reference : Answered by Nico

Code_Yoga
  • 2,968
  • 6
  • 30
  • 49
33

This usually happens when you have a file e.g. foo_test.go empty or without package declaration.

Alessandro Resta
  • 1,102
  • 1
  • 19
  • 26
  • 1
    Thank you so much. All the other answers weren't working out for me and I couldn't have figured this out either. Such an unhelpful error message. – Alf Moh Aug 08 '20 at 08:12
27

Just save the file first and than run the cammand.it is working.

go run main.go

Javed
  • 407
  • 5
  • 6
15

The problem wasn't neither with GOROOT nor GOPATH. The go installation failed at some point, leaving the whole thing unstable ( files created but completely empty ). When provisioning the virtual machine again, the go module checked whether the files existed. As they did, it took by granted that the installation had already take place.

A clean up and fresh installation from scratch solved the problem.

ThisIsErico
  • 1,885
  • 2
  • 19
  • 24
11

With gopls (v0.4.0 at the time of writing, so pretty unstable!) and vscode doing cmd+shift+P > Go: Restart language server worked for me.

Lucat
  • 2,242
  • 1
  • 30
  • 41
  • Hi there, I've tried this solution and it resulted in the error `Command 'Go: Restart Language Server' resulted in an error (command 'go.languageserver.restart' not found)`. Any idea why this might be? – Lou Jun 21 '20 at 11:41
  • 1
    Actually never mind, it just turns out my environment variables weren't configured correctly. I changed the path and now it works! – Lou Jun 21 '20 at 12:08
  • It doesn't work but gave an idea to reload the VSCode, then the problem has solved. – M. Rostami Dec 10 '20 at 14:45
10

I have faced exactly same issue today while running golang in vscode.

enter image description here

Error

enter image description here

This usually happen when you don't save code and run code directly thinking IDE like Intellij does autosave for us, but in vscode you can enable autosave to avoid this kind of error and save some time.

Go to File -> Auto save

kris
  • 979
  • 11
  • 15
5

A separate Go file in the same package,didn't have the "package main" declaration and because of this the console was giving errors on running the Main GO file.

On providing the package main declaration to the other Go file ,the error stopped showing.

srt111
  • 1,079
  • 11
  • 20
  • Why the downvote?...If there is some doubt about the above solution, try keeping an Undeclared .go file in the same package folder .The console flags " expected package ,found EOF" error (as asked in the above question) – srt111 Aug 19 '19 at 04:59
  • 1
    This was the problem for me. it was such a simple miss but this comment helped, thank you. – Manny T Dec 12 '19 at 15:25
3

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")
}
tdensmore
  • 667
  • 1
  • 6
  • 19
2

In my case, go.mod was caching the older version of it also. causing file to not save itself.

resolved the conflict locally. and did a File -> Auto Save

sumitya
  • 2,631
  • 1
  • 19
  • 32
2

If this issue occurs on hitting Ctrl+S after commenting the entire code, Just uncomment package <package_name> line and hit Ctrl+S. Issue resolves

karthikeyan
  • 195
  • 1
  • 10
1

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.

Muthulakshmi M
  • 651
  • 7
  • 8
0

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.

sonu1986
  • 211
  • 3
  • 4
0

just remember to Ctrl save before running your program and you are all good to go

0

In my case it was empty main.go file.

I have small http server with following file structure:

├── cmd
│   └── server
│       └── main.go
└── internal
    └── server
        ├── http.go
        ├── log.go
        └── main.go  <--- this one was empty

While running server with go run ./cmd/server/main.go server referenced to internal package and it was the reason for: expected 'package', found 'EOF'.

Removing internal/server/main.go fixed the issue.

zooblin
  • 2,172
  • 2
  • 27
  • 33
-2

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.

kpie
  • 9,588
  • 5
  • 28
  • 50