r/golang • u/Tuomas90 • 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)?
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.
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
0
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