December 28, 2023
Tutorial: Getting started with multi-module workspaces - The Go Programming Language
아래의 명령어를 통해 새로운 Module 생성
go mod init example.com/hello
사용할 외부 Dependency 추가
go get golang.org/x/example/hello/reverse
hello 모듈을 위한 코드 작성
package main
import (
"fmt"
"golang.org/x/example/hello/reverse"
)
func main() {
fmt.Println(reverse.String("Hello"))
}
Workspace를 Initialize하면, go.work 라는 파일이 생성됨
go work init ./hello
go.work 파일은 아래와 같은 내용을 포함하고 있으며, go.mod와 역할이 유사
go 1.21.5
use ./hello
use directive는 Build 할 때 main Module이 hello 모듈에 포함되어 있음을 나타냄
Workspace를 생성하고 나면 아래의 명령어를 통해 실행 가능
go run ./hello
golang.org/x/example/hello moduleGit Repo로부터 모듈을 다운로드 받고, 이를 Workspace에 추가 후 새로운 function을 추가할 것임
Clone Repo
git clone https://go.googlesource.com/example
Module을 workspace에 추가
go work use ./example/hello
go.work 파일이 다음과 같이 수정됨
go 1.21.5
use (
./example/hello
./hello
)
새로운 함수 추가
package reverse
import "strconv"
// Int returns the decimal reversal of the integer i.
func Int(i int) int {
i, _ = strconv.Atoi(String(strconv.Itoa(i)))
return i
}
hello.go 프로그램 수정
package main
import (
"fmt"
"golang.org/x/example/hello/reverse"
)
func main() {
fmt.Println(reverse.String("Hello"), reverse.Int(24601))
}