3

I'm new to Golang. I have multiple apps residing in the same folder in a mono repository.

apps
   app1
      go.mod
      go.sum
      main.go
   app2
      go.mod
      go.sum
      main.go
   app3
      go.mod
      go.sum
      main.go

There is also a go.work file in root directory that wraps these apps in a use block. The apps are AWS Lambda functions that are deployed independently.

How can I create a shared library that can be used by each app? I have some code that should be made reusable or else I'll end up copy-pasting everything. I've tried adding a shared.go file under apps with no luck.

user246392
  • 2,661
  • 11
  • 54
  • 96
  • 2
    Follow the tutorial [How To Write Go Code](https://go.dev/doc/code): Create a module at the root repository. Remove the other go.mod files and go.work. A section of the tutorial describes how to import a local module. https://go.dev/doc/code#ImportingLocal – Charlie Tumahai Dec 06 '22 at 07:11
  • The tutorial mentions the following: `A module is a collection of related Go packages that are released together`. I need to release each app separately. Is it possible to build and package them individually if they are part of the same module? – user246392 Dec 06 '22 at 07:56
  • 1
    Modules are the unit for version control, not for building or deployment. If you plan to version the applications and shared library as one unit, then it's simpler to put everything in single module. Packages are the unit for building. You can build multiple applications out of a single module. If you do plan to version the applications and shared library independently of each other, then it's simpler to create multiple repos with a go.mod at the root of each repo. – Charlie Tumahai Dec 06 '22 at 08:24
  • Thanks for the direction. I'll accept your answer if you post one. – user246392 Dec 07 '22 at 01:32

0 Answers0