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

84,812

Solution 1

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.

Solution 2

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

Reference : Answered by Nico

Solution 3

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

Solution 4

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

go run main.go

Solution 5

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.

Share:
84,812
ThisIsErico
Author by

ThisIsErico

Updated on May 24, 2021

Comments

  • ThisIsErico
    ThisIsErico about 3 years

    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!