# Go Runtime module file structure

**URL:** <https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497>\
**Category:** Runtime Framework\
**Tags:** golang, getting-started\
**Created:** [October 24, 2023, 1:23pm UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497 "2023-10-24T13:23:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkopacka](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@lkopacka](https://forum.heroiclabs.com/u/lkopacka)\
**Post date:** [October 24, 2023, 1:23pm UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/1 "2023-10-24T13:23:34Z")

</div>

Is it possible to have all modules as submodules of main.go?  
I want to create something like this:

Project

- main.go
- go.mod
- docker-compose.yml
- Dockerfile
- modules
- 
  - module1.go

- 
  - module2.go

- 
  - …

I am trying to do this on Windows and when I try to build it into docker im getting this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/e/e736bbc5b4352e177c9a04d7dfaca6cbbd864899.png)

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 24, 2023, 2:02pm UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/2 "2023-10-24T14:02:40Z")

</div>

Hello @lkopacka,

A Go module is a collection of packages that live under the same root defined by the `go.mod` file. Typically a Go project will only contain a single module with all the necessary source files at the root.

Any subfolder within the root would contain its own package. A package should have the same name as its parent folder and contain only source files related to it. All defined functions, types, variables and constants are visible within the package (all its source files), but only the ones exported (capitalized) are accessible when using (importing) the package.

Typically you’d define packages with reusable interfaces for very specific domains (see how the Go standard lib’s modules are things like `http`, `crypto`, `json` and so on). This also makes it easier to test these individual packages.

So depending on what you mean with `modules`, you’d need split these into separate packages, or otherwise just have a single package with some logical split between source files. I’d advise you against trying to split things into too many packages, especially if you’re not exactly sure on what interfaces you’ll need to export yet.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![lkopacka](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@lkopacka](https://forum.heroiclabs.com/u/lkopacka)\
**Post date:** [October 25, 2023, 8:12am UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/3 "2023-10-25T08:12:47Z")

</div>

Those are my project files, and I’m building them in Docker. But I’m getting:  
Error:  
main.go:6:2: package myproject/profile is not in GOROOT (/usr/local/go/src/myproject/profile)

I am trying to ask why this is happening when all the rules mentioned above seem in order?

```auto
myproject/
    ├── main.go
    ├── go.mod
    ├── Dockerfile
    └── profile/
      └── profile.go

```

main.go:

```auto
    package main

    import (
        "context"
        "database/sql"
        "myproject/profile"

        "github.com/heroiclabs/nakama-common/runtime"
    )    

    func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
        if err := initializer.RegisterRpc("profile_update_metadata", profile.UpdateMetadata); err != nil {
            logger.Error("Unable to update profile's metadata, reason: %v", err)
            return err
        }
        return nil;
    }

```

profile.go

```auto
    package profile

    import (
        "context"
        "github.com/heroiclabs/nakama-common/runtime"
    )

    func UpdateMetadata(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
        ....
        return "{\"Success\":0}", nil
    }

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 25, 2023, 8:17am UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/4 "2023-10-25T08:17:41Z")

</div>

What’s the content of your `go.mod` file?

---

<div class="post-metadata">

**Author:** ![lkopacka](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@lkopacka](https://forum.heroiclabs.com/u/lkopacka)\
**Post date:** [October 25, 2023, 8:28am UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/5 "2023-10-25T08:28:51Z")

</div>

go.mod

```auto
module myproject

go 1.21

require github.com/heroiclabs/nakama-common v1.28.1

require google.golang.org/protobuf v1.31.0 // indirect

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 25, 2023, 9:28am UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/6 "2023-10-25T09:28:40Z")

</div>

I believe in profile.go you’re missing the import for “database/sql”.

Please try setting the go version to `go 1.19` in the `go.mod` file and try again.

---

<div class="post-metadata">

**Author:** ![lkopacka](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@lkopacka](https://forum.heroiclabs.com/u/lkopacka)\
**Post date:** [October 25, 2023, 10:43am UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/7 "2023-10-25T10:43:50Z")

</div>

Yes, did miss “database/sql” when copying, but it is there.  
And I did downgrade the go version in go.mod to 1.19  
But the outcome is:

```auto
$ docker compose up --build
[+] Building 0.9s (12/14)
 => [internal] load build definition from Dockerfile 0.0s 
 => => transferring dockerfile: 32B 0.0s 
 => [internal] load .dockerignore 0.0s 
 => => transferring context: 2B 0.0s 
 => [internal] load metadata for registry.heroiclabs.com/heroiclabs/nakama:3.18.0 0.4s 
 => [internal] load metadata for docker.io/heroiclabs/nakama-pluginbuilder:3.18.0 0.5s 
 => [go-builder 1/6] FROM docker.io/heroiclabs/nakama-pluginbuilder:3.18.0@sha256:c88a79f47824c3391e6ff2a09ae5f105b2dfbc367b5f162d0939daa4c00e0ecf 0.0s 
 => [stage-1 1/3] FROM registry.heroiclabs.com/heroiclabs/nakama:3.18.0@sha256:5091f870a6e4e2b3cfa1dd67c7f09fa913e601d2413fe6ae29da2e2f7d3e43ab 0.0s 
 => [internal] load build context 0.0s 
 => => transferring context: 14.15kB 0.0s 
 => CACHED [go-builder 2/6] WORKDIR /backend 0.0s 
 => CACHED [go-builder 3/6] COPY go.mod . 0.0s 
 => CACHED [go-builder 4/6] COPY *.go . 0.0s 
 => CACHED [go-builder 5/6] COPY vendor/ vendor/ 0.0s 
 => ERROR [go-builder 6/6] RUN go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so 0.3s 
------
 > [go-builder 6/6] RUN go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so:
#0 0.303 main.go:6:2: package myproject/profile is not in std (/usr/local/go/src/myproject/profile)
------
failed to solve: executor failed running [/bin/sh -c go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so]: exit code: 1

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 25, 2023, 12:18pm UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/8 "2023-10-25T12:18:02Z")

</div>

I think you’re not copying the package directory into the docker container, add:

```auto
COPY profile/ profile/

```

to your Dockerfile

---

<div class="post-metadata">

**Author:** ![lkopacka](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@lkopacka](https://forum.heroiclabs.com/u/lkopacka)\
**Post date:** [October 25, 2023, 12:31pm UTC](https://forum.heroiclabs.com/t/go-runtime-module-file-structure/4497/9 "2023-10-25T12:31:59Z")

</div>

Yep, that’s it. Thanks a lot.
