Friday, October 10, 2025
HomeGolangGo native modules - Getting Assist

Go native modules – Getting Assist


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:

  1. make empty new work-directory work
  2. make listing for greetings-module work/greetings (package deal greetings)
  3. run go mod init instance.com/greetings on this listing. This creates a go.mod file for this module.
  4. paste code to work/greetings/greetings.go
  5. make listing for hello-module work/hi there/ (package deal foremost)
  6. run go mod init instance.com/hi there on this listing. Once more a go.mod file is made.
  7. edit work/greetings/go.mod in order that it considers each import to instance.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 the go.mod-file. VERY COOL)
  8. run go run hi there.go in work/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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments