Setting Up The Environment: I Feel Very Frustrating – Getting Help

Greetings to everyone.
However being an IT Engineer for more than 20 years, having developed in C, C++, C#, Python, Ada, Java… I am totally new to Golang but few days ago, I am working on a REST API wanted to make and I said for me… hey this is an opportunity to learn something new, so let’s go…
yeah ok lame joke, let’s get to the point:

I can’t even start coding because the environment just doesn’t work. I have exactly the same problems on windows and linux.

  • I installed golang v1.19 (latest), it was pretty straight forward

  • I created a simple directory… T:\TestAPI

  • I created a main.go file with the original “hello world” in that directory and I just ran “go run main.go”. It went perfectly fine. I also compiled with “go build” and no problems so far.

  • I started testing a dummy API with Gorilla/Mux and Lib/PQ… and the nightmare started:

  • I have set GOPATH to T:\TestAPI

  • I tried to install mux and pq with “go get” command but it said that
    1/ go get is obsolete and i must use go install, with @latest suffix
    2/go.mod file not found in current directory

  • I installed mux and pq via go install … and it seemed to work … pkg subdirectory was created and the code inside.

  • I tried “go run” on my main.go file, and it said: no required module provides package github.com/gorilla/mux: go.mod file not found in current directory or any parent directory; see 'go help modules'

  • Fair enough, I made a module…

T:\TestAPI>go run main.go
main.go:10:2: no required module provides package github.com/gorilla/mux: go.mod file not found in current directory or any parent directory; see 'go help modules'
main.go:11:2: no required module provides package github.com/lib/pq: go.mod file not found in current directory or any parent directory; see 'go help modules'

T:\TestAPI>go mod init TestAPI
go: creating new go.mod: module TestAPI
go: to add module requirements and sums:
        go mod tidy

T:\TestAPI>go mod tidy
$GOPATH/go.mod exists but should not

T:\TestAPI>go run main.go
$GOPATH/go.mod exists but should not

So basically… go wants me to make a go.mod file until it runs my code, and once the go.mod file is there, it says it shouldn’t be there. hot_face: Boom:

And since then, it’s been a nightmare. I had a friend who told me it worked in his favor, but he is using an older version of golang (1.12 I think) and it seems to have evolved a lot on the environment side in previous versions.
On my linux machine, I could set GOPATH as empty and that seemed to solve the problem (and I still don’t understand why), but in my windows machine, empty gopath doesn’t work Is …

I’ve been looking for the answer by myself for some time… I’m sure I’m missing something stupid, and it’s really frustrating: I don’t understand how this is supposed to be done.
I am sure you guys will be able to help me in 2 seconds…
Many thanks in advance.
encourage,
american


Leave a Comment