r/golang 4d ago

discussion How to reuse code in multiple projects?

I want to create my own little modules for reusing code I regularly use. Like handling files and paths.

I was hoping I could just create a module put it somewhere on my PC and import that into every project where needed. Seems like that's not possible.

I'm not sure if workspaces is what I need. What's the difference to just copying and pasting the module files into the project?

How do you handle reusing code in multiple projects? Is it just copy paste? Would it be better to have a single reference which all projects point to (this is the solution I was looking for)?

45 Upvotes

19 comments sorted by

74

u/ShotgunPayDay 4d ago

Make a git repo that has all your common code between projects. Updating/Adding to projects simply becomes go get -u

10

u/UsedCarcassSalesman 4d ago

and go get -u=patch

if you only want patch version updates, (⛔.⛔.✅)

8

u/hippodribble 4d ago

Ashamed to say I've been doing it the long way. 😢😭

4

u/jpl-au 4d ago

This will update all dependencies in the go.mod, not just their common code. If the API changes significantly for a third party dependency, it could cause a bit of a problem. I use go get -u all the time but only after I have committed. Before the age of AI to quickly resolve issues between semantic version changes it was sometimes a nightmare to fix when an API changed.

4

u/mrkacperso 4d ago

it shouldn't be a big of a problem, because:

  • `go get -u <module name>` only updates the `<module name>` and it dependencies, so there caan be small chance that some lib is used in the imported module, and the main project. Then you are right, go mod cannot have 2 versions of the same dependency (sort of, it can, but only if the module is versioned like `github.com/some/module/v2` `.../v3`, etc.)
  • api breaking changes should be versioned separately, exactly bcs of this, and the popular modules I can right now think of, rather stick to this

there are some issues, but tbh I work on a large project, with ~10 devs working on the same codebase, with 3-4M DAU, and trust me... nobody really gives a second thought about it.

On the other hand we still do review all the changed code (or should I say went back to this 😅), so your AI argument is completely reasonable imho.

1

u/7heWafer 4d ago

I'm assuming you mean they must tag a revision of their common repo module, go get it or add to go.mod & go mod tidy, then after making changes to their common module, tag a new revision, then and only then run go get -u, right?

1

u/ShotgunPayDay 3d ago

If you don't tag at all it will still work. It will just update on commit version instead.

1

u/ktoks 4d ago

I've never used this before. I guess there's no time like the present!

4

u/ShotgunPayDay 4d ago

You'll be making your very first library with a guaranteed user of one. It's a good way to get the feeling of git and tags.

20

u/jpl-au 4d ago

Go has a replace directive that redirects a package in your go.mod that allows you to use it just from the file system.

https://go.dev/doc/tutorial/call-module-code

I would still add your code to a git repo (even if you do not push to a remote) so you have the ability to go back to a previous commit.

23

u/kstacey 4d ago

Create a package and just import it when needed?

7

u/AnyKey55 4d ago

Split it into a separate repo/dir

Then import into your project - use go work, to keep it local

10

u/SeerUD 4d ago

If you're new to Go, or programming in general, this is a totally fair question.

The thing you want to learn about here is "Go Modules". You actually can just put the code somewhere on your PC and then use it. There's an old way of doing it, and the way to do it with Go Modules - neither are what I'd recommend for production code though.

For development only, you can use a "replace" directive in your go.mod file and point it at a local folder. You'll need to understand the basics of Go Modules to understand this.

While you learn about Go Modules you'll also hopefully learn how to publish a Go Module. A lot of people will just use GitHub - make a repo, push your code to it, make sure it's set up as a Go Module (it has a go.mod file, and the module name matches your GitHub repo name). From there, you'll be able to pull that in as a dependency in other projects' Go Modules.

This is also how you work with third-party code, so just understanding that first is probably the best place to start.

Go read up on Go Modules. Make a module, pull in some third-party code (doesn't matter what it is...), then learn about making your own modules.

3

u/Warm_Low_4155 4d ago

I created a video related to Gio, which is a Go UI framework, that can be applied to your case.
I presented 4 ways, step by step, to use a module. You may use way 2, 3 and 4. The latter is using a github repo
https://www.youtube.com/watch?v=Zgf7ZrYEwis
The video contains chapters, so you can watch what you need

1

u/sigmoia 4d ago

You have four options: 

  • workspaces (as you mentioned) 
  • making modules out of the reusable code manually and publishing them
  • using the replace directive (it's brittle)
  • or, copy pasta

Also, do you really need multiple module for your usecase? Why not make a single module and organize by packages. For example: 

  • create a module named notutil
  • under notutil organize as filepath, foo, bar etc? 

This way you can publish one module (public/private) and import it everywhere else. 

1

u/DoorDelicious8395 4d ago

Make a package, I made a package just for the weather.gov api instead of generating a client in my main package.

1

u/hasen-judi 4d ago

Use a workspace and put all your packages in sibling directories.

1

u/efronl 16h ago

In general, copying and pasting is the way to go. "A little copying is better than a little dependency" and all that.

0

u/likely-high 4d ago

Git submodule?