How to import module package in Golang

wjswldhks123·2022년 3월 28일


목록 보기

First thing first, you have to check that your GitHub repository is “public” or “private”.

For example,

│ └─workflows
│ └─main
│ ├─generated
│ └─model
│ ├─config
│ ├─controllers
│ ├─models
│ ├─routes
│ └─utils

  • Public

At the top-level in directory(root), $go mod init

If I want to import pkg/controllers, just import

In my case, it’s gonna be

  • Private

However, you have to do it in a different way in a private git repository.

Firstly, it is same way as public. At the top-level in directory(root), $go mod init
You have to make another mod file in a specific folder whatever you want to modulize.

For example, inside the ‘pkg’ package, cd pkg, you have to $go mod init In my case, go mod init After that, you have to modify the root go.mod file. You have to add this line “replace => ./pkg

‘replace’ asks to replace the mentioned package with the path that you mentioned. So, it won’t further look at packages elsewhere and would look inside that’s pkg package located there itself.

Refer to my git repository,
The above my git repository is “public”.

Jiwan Jeon

0개의 댓글