Wednesday, 30 December 2020

Microsoft Visual Studio Code and Go - compiler says "No" - "main redeclared in this block" -

 So I had a bit of a face-palm moment this afternoon, whilst discussing how to configure Microsoft Visual Studio Code ( aka VS Code ) with Go on macOS 11 Big Sur.

Having updated Go to the most recent version: -

go version

go version go1.15.6 darwin/amd64

I was trying/failing to run a newly created Hello World Go module within VS Code itself: -

hello-world.go 

package main

import "fmt"


func main() {

fmt.Printf("hello, world\n")

}

However, VS Code just returns: -

# hello
/Users/hayd/go/src/hello/hello.go:5:6: main redeclared in this block
previous declaration at /Users/hayd/go/src/hello/hello-world.go:5:6
Build process exiting with code: 2 signal: null

within the Debug Console and, within the Problems perspective, I see: -


Now it **SHOULD** be obvious .....

But I'm obviously getting old ....

Can you see what I'm doing wrong ? I bet you can ....

Having validated that the module ran OK from the Terminal: -

cd $GOPATH/src/hello

go run hello-world.go 

hello, world

I again missed the obvious: -

ls -al

total 16
drwxr-xr-x  5 hayd  staff  160 30 Dec 17:27 .
drwxr-xr-x  9 hayd  staff  288 11 Sep 10:50 ..
-rw-r--r--  1 hayd  staff   74 30 Dec 17:27 hello-world.go
-rw-r--r--  1 hayd  staff   74 20 Feb  2020 hello.go
drwxr-xr-x  2 hayd  staff   64 20 Feb  2020 vendor

cat hello.go

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

cat hello-world.go

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

To put you, and me, out of our collective miseries, my mistake was ....

HAVING TWO MODULES IN THE SAME FOLDER, BOTH OF WHICH ARE DEFINING THE MAIN METHOD !!!!

Exactly as the VS Code errors were telling me: -

other declaration of main

main redeclared in this block

Yep, once I nuked the second instance: -

rm hello-world.go

VS Code was happy: -


and the remaining hello.go module ran happily: -


Can you say "Doofus" ? I bet you can ......

No comments:

Note to self - use kubectl to query images in a pod or deployment

In both cases, we use JSON ... For a deployment, we can do this: - kubectl get deployment foobar --namespace snafu --output jsonpath="{...