How to import module package in Golang

wjswldhks123·2022년 3월 28일
0

Golang

목록 보기
4/4


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

For example,

├─.github
│ └─workflows
├─cmd
│ └─main
├─graph
│ ├─generated
│ └─model
├─pkg
│ ├─config
│ ├─controllers
│ ├─models
│ ├─routes
│ └─utils
└─public
├─javascripts
└─stylesheets

  • Public

At the top-level in directory(root), $go mod init github.com/username/filename.

If I want to import pkg/controllers, just import github.com/username/filename/pkg/controllers.

In my case, it’s gonna be github.com/jiwanjeon/go_todolist/pkg/controllers.

  • 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 github.com/username/filename.
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 github.com/username/filename/foldername. In my case, go mod init github.com/jiwanjeon/go_todolist/pkg. After that, you have to modify the root go.mod file. You have to add this line “replace github.com/jiwanhjeom/go_todolist/pkg => ./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, https://github.com/jiwanjeon/golang-todolist.
The above my git repository is “public”.

profile
Jiwan Jeon

0개의 댓글