# Go local modules

**URL:** <https://forum.golangbridge.org/t/go-local-modules/33256>\
**Category:** Getting Help\
**Created:** [October 7, 2023, 12:33am UTC](https://forum.golangbridge.org/t/go-local-modules/33256 "2023-10-07T00:33:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopal\_Kankanhalli](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/gopal_kankanhalli/32/14119_2.png) [@Gopal\_Kankanhalli](https://forum.golangbridge.org/u/Gopal_Kankanhalli)\
**Post date:** [October 7, 2023, 12:33am UTC](https://forum.golangbridge.org/t/go-local-modules/33256/1 "2023-10-07T00:33:53Z")

</div>

Following the steps outlined in the below doc did not work with 1.21.

> **[Call your code from another module - The Go Programming Language](https://go.dev/doc/tutorial/call-module-code)**

All help appreciated.

---

<div class="post-metadata">

**Author:** ![duckduck](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/duckduck/32/14101_2.png) [@duckduck](https://forum.golangbridge.org/u/duckduck)\
**Post date:** [October 7, 2023, 7:12am UTC](https://forum.golangbridge.org/t/go-local-modules/33256/2 "2023-10-07T07:12:26Z")

</div>

I just tried it on `1.21.1` and works for me.  
Your link points to the middle of a tutorial. Have you checked the previous section where they make the `greetings`-module?

Anyhow, this what you end up with:

```auto
.
├── greetings
│ ├── go.mod // defaults of this file 
│ │ // (created by go mod init) are good
│ └── greetings.go // package greetings AKA a library/module
└── hello
   ├── go.mod // uses a replace-directive that tells go-code
   │ // in this package where "example.com/greetings" 
   │ // can be found locally in stead of online
   └── hello.go // package main AKA an executable with a main-func

```

Here are the steps I did:

1. make empty new work-directory `work`
2. make directory for **greetings-module** `work/greetings` (`package greetings`)
3. run `go mod init example.com/greetings` in this directory. This creates a `go.mod` file for this module.
4. paste code to `work/greetings/greetings.go`
5. make directory for **hello-module** `work/hello/` (`package main`)
6. run `go mod init example.com/hello` in this directory. Again a `go.mod` file is made.
7. edit `work/greetings/go.mod` so that it considers every import to `example.com/greetings` that it finds in code, to actually use `../greetings` ( **this was new for me** , it’s very cool that this is possible, before I was just editing my `.go`-code but apparently you can _redirect_ with the `go.mod`-file. **VERY COOL** )
8. run `go run hello.go` in `work/hello`. The (quite amazing) go build system will do all the rest.

For step 7, it was new for me that we can use `go mod edit` to (more safely) edit the go.mod-file. I had done some manual editing of that file in the past but it was quite interesting to read `go help mod edit`.

---

<div class="post-metadata">

**Author:** ![duckduck](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/duckduck/32/14101_2.png) [@duckduck](https://forum.golangbridge.org/u/duckduck)\
**Post date:** [October 7, 2023, 7:15am UTC](https://forum.golangbridge.org/t/go-local-modules/33256/3 "2023-10-07T07:15:52Z")

</div>

For reference, this is what my `hello/go.mod` file looks like after the command.

`> go mod edit -replace example.com/greetings=../greetings`

```auto
module example.com/hello

go 1.21.1

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

```

---

<div class="post-metadata">

**Author:** ![Gopal\_Kankanhalli](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/gopal_kankanhalli/32/14119_2.png) [@Gopal\_Kankanhalli](https://forum.golangbridge.org/u/Gopal_Kankanhalli)\
**Post date:** [October 7, 2023, 12:22pm UTC](https://forum.golangbridge.org/t/go-local-modules/33256/4 "2023-10-07T12:22:00Z")

</div>

Thanks. But according to the tutorial we are supposed to change the calling module hello.  
From the command prompt in the hello directory, run the following command:

$ go mod edit -replace [example.com/greetings=../greetings](http://example.com/greetings=../greetings)

---

<div class="post-metadata">

**Author:** ![duckduck](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/duckduck/32/14101_2.png) [@duckduck](https://forum.golangbridge.org/u/duckduck)\
**Post date:** [October 7, 2023, 1:36pm UTC](https://forum.golangbridge.org/t/go-local-modules/33256/5 "2023-10-07T13:36:37Z")

</div>

Yes, we are changing `work/hello/go.mod`. As you can see in my code fragment of `go.mod` on the first line, this is the module we are changing.

The `replace`-statements basically take care of changing `import "example.com/greetings"` in `hello.go` on the fly to `import "../greetings"`. Manually changing the imports in `hello.go` like that, would be an alternative solution but I think it’s nice that `go mod` supports this replace-mechanism.

---

<div class="post-metadata">

**Author:** ![Gopal\_Kankanhalli](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/gopal_kankanhalli/32/14119_2.png) [@Gopal\_Kankanhalli](https://forum.golangbridge.org/u/Gopal_Kankanhalli)\
**Post date:** [October 7, 2023, 1:50pm UTC](https://forum.golangbridge.org/t/go-local-modules/33256/6 "2023-10-07T13:50:28Z")

</div>

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![duckduck](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/duckduck/32/14101_2.png) [@duckduck](https://forum.golangbridge.org/u/duckduck)\
**Post date:** [October 7, 2023, 2:22pm UTC](https://forum.golangbridge.org/t/go-local-modules/33256/7 "2023-10-07T14:22:45Z")

</div>

No problem. It was nice discovering this functionality of `go mod`.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [January 5, 2024, 2:23pm UTC](https://forum.golangbridge.org/t/go-local-modules/33256/8 "2024-01-05T14:23:16Z")

</div>

Closed on January 5, 2024, 2:23pm UTC

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