ThisIsErico
ThisIsErico

Reputation: 2037

Go failing - expected 'package', found 'EOF'

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!

Upvotes: 83

Views: 181578

Answers (16)

Steve Wu
Steve Wu

Reputation: 193

In my case, I mistakenly set the third parameter to an empty string

    f, err := parser.ParseFile(fset, targetFile,"", parser.AllErrors)
    if err != nil {
        return fmt.Errorf("ParseFile %s: %w", targetFile, err)
    }

After replace the "" with nil the problem is gone.

    f, err := parser.ParseFile(fset, targetFile, nil, parser.AllErrors)
    if err != nil {
        return fmt.Errorf("ParseFile %s: %w", targetFile, err)
    }

Upvotes: 1

zooblin
zooblin

Reputation: 2490

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.

Upvotes: 0

karthikeyan
karthikeyan

Reputation: 235

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

Upvotes: 3

sumitya
sumitya

Reputation: 2691

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

Upvotes: 2

Abubakar Ijaz
Abubakar Ijaz

Reputation: 51

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

Upvotes: 0

kris
kris

Reputation: 1219

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

Upvotes: 12

Muthulakshmi M
Muthulakshmi M

Reputation: 787

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.

Upvotes: 1

Luke
Luke

Reputation: 2474

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.

Upvotes: 11

Javed
Javed

Reputation: 437

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

go run main.go

Upvotes: 28

srt111
srt111

Reputation: 1551

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.

Upvotes: 7

sonu1986
sonu1986

Reputation: 231

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.

Upvotes: 0

Code_Yoga
Code_Yoga

Reputation: 3278

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

Reference : Answered by Nico

Upvotes: 175

kpie
kpie

Reputation: 11120

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.

Upvotes: -2

tdensmore
tdensmore

Reputation: 816

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")
}

Upvotes: 3

Alessandro Resta
Alessandro Resta

Reputation: 1182

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

Upvotes: 38

ThisIsErico
ThisIsErico

Reputation: 2037

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.

Upvotes: 17

Related Questions