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

推荐订阅源

J
Java Code Geeks
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
H
Help Net Security
I
InfoQ
月光博客
月光博客
量子位
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
D
DataBreaches.Net
宝玉的分享
宝玉的分享
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理

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.