Following the steps outlined within the beneath doc didn’t work with 1.21.
All assist appreciated.
I simply tried it on 1.21.1
and works for me.
Your hyperlink factors to the center of a tutorial. Have you ever checked the earlier part the place they make the greetings
-module?
Anyhow, this what you find yourself with:
.
├── greetings
│ ├── go.mod // defaults of this file
│ │ // (created by go mod init) are good
│ └── greetings.go // package deal greetings AKA a library/module
└── hi there
├── go.mod // makes use of a replace-directive that tells go-code
│ // on this package deal the place "instance.com/greetings"
│ // might be discovered domestically in stead of on-line
└── hi there.go // package deal foremost AKA an executable with a main-func
Listed here are the steps I did:
- make empty new work-directory
work
- make listing for greetings-module
work/greetings
(package deal greetings
) - run
go mod init instance.com/greetings
on this listing. This creates ago.mod
file for this module. - paste code to
work/greetings/greetings.go
- make listing for hello-module
work/hi there/
(package deal foremost
) - run
go mod init instance.com/hi there
on this listing. Once more ago.mod
file is made. - edit
work/greetings/go.mod
in order that it considers each import toinstance.com/greetings
that it finds in code, to truly use../greetings
(this was new for me, it’s very cool that that is attainable, earlier than I used to be simply modifying my.go
-code however apparently you possibly can redirect with thego.mod
-file. VERY COOL) - run
go run hi there.go
inwork/hi there
. The (fairly wonderful) go construct system will do all the remainder.
For step 7, it was new for me that we will use go mod edit
to (extra safely) edit the go.mod-file. I had finished some guide modifying of that file prior to now nevertheless it was fairly fascinating to learn go assist mod edit
.
For reference, that is what my hi there/go.mod
file seems like after the command.
> go mod edit -replace instance.com/greetings=../greetings
module instance.com/hi there
go 1.21.1
exchange instance.com/greetings => ../greetings
require instance.com/greetings v0.0.0-00010101000000-000000000000
Thanks. However in line with the tutorial we’re supposed to vary the calling module hi there.
From the command immediate within the hi there listing, run the next command:
$ go mod edit -replace instance.com/greetings=../greetings
Sure, we’re altering work/hi there/go.mod
. As you possibly can see in my code fragment of go.mod
on the primary line, that is the module we’re altering.
The exchange
-statements mainly handle altering import "instance.com/greetings"
in hi there.go
on the fly to import "../greetings"
. Manually altering the imports in hi there.go
like that, can be an alternate resolution however I believe it’s good that go mod
helps this replace-mechanism.
No drawback. It was good discovering this performance of go mod
.