惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
J
Java Code Geeks
有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
量子位
D
Docker
V
Visual Studio Blog
博客园 - 司徒正美
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
U
Unit 42
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
V
V2EX
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
Vercel News
Vercel News
C
Check Point Blog

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
How to setup Go packages under monorepo
Ashish Bhatia · 2023-11-05 · via ashishb.net

Let’s say you want to have two Go packages pkg1 and pkg2 in a monorepo setup. Here’s what a good project structure would look like.

Here’s my recommended project structure in that case

1
2
3
4
5
6
7
src/pkg1
|
|- go.mod
|- go.sum
|- pkg - Optional: required if pkg1 exports any code. It is just a convention to put all exported code in pkg
|- internal - most code should go here by default, code here cannot be accessed outside of pkg1
|- cmd - CLI tools or web server startup code goes here

src/pkg2’s structure would look very similar.

Now let’s assume that you are hosting this monorepo at https://github.com/ashishb/golang-template-repo. I would recommend that src/pkg1’s module name, defined in go.mod be github.com/ashishb/golang-template-repo/src/pkg1 and pkg2’s module name be github.com/ashishb/golang-template-repo/src/pkg2. This is not a hard-and-fast requirement, however, this will avoid a lot of confusion if the package name maps to the URL.

Now, let’s assume that there is code in src/pkg1/pkg/module1 that’s exported and is being used by pkg2. To add that mapping just use the replace directive and add the following to src/pkg2/go.mod.

1
2
3
replace (
    github.com/ashishb/golang-template-repo/src/pkg1 => ../pkg1/
)

Now, import github.com/ashishb/golang-template-repo/src/pkg1/pkg/module1 would just work.

As a reminder, import github.com/ashishb/golang-template-repo/src/internal would never work as internal is never exported.