Setting up the environment : I feel so frustrated

Hello everyone.
Although being an IT engineer for more than 20 years, having developped in C, C++, C#, Python, Ada, Java … I’m totally new to Golang but a couple of days ago, I wanted to create a Rest API and I said to myself … hey it’s the opportunity to learn something new, so let’s have a go … :blush:
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 (the latest), it was pretty straightforward

  • I created a simple directory … T:\TestAPI

  • I created a main.go file with basic “hello world” in that directory and i just ran “go run main.go”. It went perfectly well. I also compiled with “go build” and so far no problem.

  • I started to test a dummy api with gorilla/mux and lib/pq … and the nightmare began:

  • I set GOPATH to T:\TestAPI

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

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

  • I tried a “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 created 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 create a go.mod file otherwise it doesn’t run my code, and once the go.mod file is there, it says that it shouldn’t be there. :hot_face: :boom:

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

I’ve looked for a while for the answer by myself … I’m sure i’m missing something stupid, and it’s really frustrating : I just don’t understand how it should be done.
I’m sure that you guys will be able to help me in 2 seconds…
Thanks very much by advance.
Cheers,
Aymeric

You either use legacy GOPATH workspaces xor new stayle go.mod projects.

And a go module requires to follow a naming schema, it has to have something that looks like a hostname (with at least one dot) and 2 path segments at least.

This limitation stems from the way how most repo-providers organize their repository base-URLs.

It might even be that the path segment limitation is based on the actual hostname, though there I am not sure, as all modules I had so far have been GitHub or GitLab hosted.

So after unsetting the GOPATH or pointing it to some other folder you do not use anyway, it should work.

I would prefer unsetting it though, then Go will fall back to some OS dependant sane default, and use it for caching and installing artifacts.

2 Likes

Yep, that’s what I missed. It makes sense now !

Vielen dank, Norbert, und ein schönen Tag noch.
A

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