-2

I am learning at https://youtu.be/ma7rUS_vW9M?t=73

Video

enter image description here

My environment

enter image description here

D:\temp2023_03_01\go\src>go version
go version go1.20.1 windows/amd64

My actions

Microsoft Windows [Version 10.0.22621.1265]
(c) Microsoft Corporation. All rights reserved.

D:\temp2023_03_01\go\src\github.com\robbyklein\go-jwt>go mod init
go: cannot determine module path for source directory D:\temp2023_03_01\go\src\github.com\robbyklein\go-jwt (outside GOPATH, module path must be specified)

Example usage:
        'go mod init example.com/m' to initialize a v0 or v1 module
        'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.

D:\temp2023_03_01\go\src\github.com\robbyklein\go-jwt>

enter image description here

Why my result is different with video? What I do for archive result like the video?

Vy Do
  • 46,709
  • 59
  • 215
  • 313

2 Answers2

2

https://pkg.go.dev/cmd/go#hdr-Initialize_new_module_in_current_directory

Init accepts one optional argument, the module path for the new module. If the module path argument is omitted, init will attempt to infer the module path using import comments in .go files, vendoring tool configuration files (like Gopkg.lock), and the current directory (if in GOPATH).

Your GOPATH is set to D:\vygopath but the project for which you are running go mod init is located in D:\temp2023_03_01\go\src. If you want go mod init to work without module-path then you should move the project over to D:\vygopath\src instead. After that the go mod init command should work, the tool should be able to infer the module-path.

enter image description here

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • It did not work as you said https://user-images.githubusercontent.com/1328316/222188235-c9bbeb6d-6eb9-470e-b72f-7a5aeba294a5.png . `D:\vygopath\src\github.com\robbyklein\go-jwt>go mod init` – Vy Do Mar 01 '23 at 15:40
  • I wouldn't suggest moving a project _into_ GOPATH, since that is deprecated and the inferred module path was only to help upgrade legacy projects (and judging by the reply, it may not even work any longer) – JimB Mar 01 '23 at 15:42
  • @JimB The fact that it didn't work for OP only suggests that OP did something wrong, I think. For me, it works as documented in the linked docs: https://imgur.com/a/7v1sQX6 – mkopriva Mar 01 '23 at 15:53
  • 1
    looks like you're right it does seem to work. I only meant that _"you should move the project over to ..."_ sounds like endorsing moving the project back into `GOPATH` rather than using the correct invocation of `go mod init module.name` (even though you were technically answering the question ;)) – JimB Mar 01 '23 at 15:57
  • 1
    @RaphaëlColantonio make sure `$GOPATH` is set in the `cmd.exe` session in which you are running `go mod init`. And make sure to use _that_ `$GOPATH` (in case it's different from `D:\vygopath`) as the root of your project if you want `go mod init` to work. But, as JimB already pointed out, relying on GOPATH is out of date, specifying the module-path explicitly like you did in your answer is the recommended approach these days. – mkopriva Mar 01 '23 at 16:06
0
D:\temp2023_03_01\go\src>go mod init github.com/robbyklein/go-jwt
go: creating new go.mod: module github.com/robbyklein/go-jwt
Vy Do
  • 46,709
  • 59
  • 215
  • 313