Skip to content
GwiyeomGo Tech Blog
About GwiyeomGo

Golang(Go) golang module

GOLANG, 20222 min read

go module 은 뭐지?

Is like package.json for Go!

모듈 시스템이다.

* Golang 버전 1.11 부터 적용
* Golang 버전 1.16 기본 사양
* Golang 버전 1.17 `GOPATH` 배제,모듈만 지원

기존 방식

go env

GOPATH에 지정된 경로에 src 폴더에 프로젝트를 생성해야 했다. GOPATH를 수정하는 작업을 해야 했음

변경된 내용

GOPATH에 프로젝트를 반드시 생성할 필요가 없다.

프로젝트 모듈을 생성

원하는 위치에 test 폴더를 생성한다.

mkdir test
cd test

모듈을 생성하는 명령어

go mod init github.com/gwiyeomgo/test
//go mode init {github.com}/{userName}/{test}
ls
// 폴더 안에 목록을 보면 go.mod 모듈 파일이 생성된다.

test 프로젝트의 go.mod 파일 내부

module github.com/gwiyeomgo/test
go 1.17

test 프로젝트 github 에 올리기

1.main.go 파일 생성 2.github 의 개인 repository 생성합니다.

module 꺼내 쓰기

참조 1 참조 2

go에서 받아온 코드들을 보기좋게 정리하는 방법

cd go
cd src
도메인별로 분류를 해서 저장
ex) github.com
mkdir username
cd username
github repository이름

go.mod

go 1.11 버전부터 모듈 시스템 도입 모듈의 장점은 프로젝트를 변경 할 때 마다 GOPATH를 수정하는 작업이 필요 없다 즉 GOPATH/src 디렉토리 바깥에 프로젝트 디렉트로리를 만들 수 있음 (출처:https://ingeec.tistory.com/106)

mkdir project
cd project
go mod init github.com/username/project <-독립적인 프로젝트 경로를 생성하기위해

Golang(Go) 프라이빗 모듈 접근 2022-08-10

go get github.com/gwiyeomgo/{프로젝트}
go: downloading github.com/gwiyeomgo/{프로젝트}
404 Not Found
server response:
not found: github.com/gwiyeomgo/{프로젝트}
8: exit status 128:
fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
```
`go env -w GOPRIVATE=github.com/{프로젝트}`
> go get github.com/gwiyeomgo/{프로젝트}
```
go: downloading github.com/gwiyeomgo/{프로젝트}
go get: added github.com/gwiyeomgo/{프로젝트}
```
### 출처
https://mingrammer.com/go-modules-private-repo/
© 2024 by GwiyeomGo Tech Blog. All rights reserved.