IDENT error when running fmt

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.

1 Like

GOPATH returns C:\Users\Chris\Documents\Go Workspace

I took a screenshot. “main.go” resides under Workspace settings, but I’m thinking that might be the problem?

Go source code must reside in the ‘src’ subdirectory. ‘pkg’ is for compiled libraries only.

Try the following steps:

  1. Move your code from Go Workspace\pkg\windows_amd64\... to Go Workspace\src\...
  2. Then cd into the directory of main.go
  3. Call go fmt .
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.