I’m learning from a Udemy class and the guy isn’t using VSC, so it’s a bit difficult. I’m on the “Hello World” task, but it doesn’t seem to be registering for me. Here’s the code that I’m putting into VSC:
package main
import “fmt”
func main() {
fmt.Println(“Hello World!”)
}
But then when I go to either the terminal in VSC or my other one in Git Bash, I get an this message: PS C:\Users\Chris> go fmt
main.go:7:1: expected declaration, found ‘IDENT’ function
exit status 2
Maybe I have it pointed to the wrong folder - so far, we’ve been putting everything in a “Go Workspace” that’s different from “C:\Users\Chris” but I don’t know how to change that, or if I even should.
You wrote that the Go workspace is different from C:\Users\Chris, yet you seem to call go fmt in that directroy. This is most likely cause for the error.
What does the command
go env GOPATH
return? This is the path to the Go workspace, and your Go source files should reside underneath this directory. You can change this location by creating an environment variable GOPATH that points to the desired location. (Ensure to start a new command prompt afterwards to pick up the new variable.)
go fmt expects the import path of a package, so assuming your main.go resides under %GOPATH%\yourproject, try calling go fmt as
go fmt yourpackage
or cd into this directory and call
go fmt .
That being said, if you use VSC with the Go plugin, your code should automatically get go fmted every time you save a .go file.