1.
2. 버전 및 환경 변수 설정 확인
> go version
go version go1.19 windows/amd64
> go env
set GOROOT=C:\Program Files\Go
set GOPATH=C:\Users\admin\workspace\golang
set GO111MODULE=on
....
GOROOT
GO 툴이 설치된 경로
C:\Program Files\Go
GOPATH
The GOPATH environment variable specifies the location of your workspace
GOPATH환경 변수는 작업 공간의 위치를 지정, 현재 만들고 있는 GO 프로그램 위치 (workspace)
- src : 소스 파일(패키지)를 컴파일하여 실행 파일(바이너리)이 생성되는 디렉터리
- pkg : 패키지를 컴파일하여 라이브러리 파일이 생성되는 디렉터리
- bin : 내가 작성한 소스 파일과 인터넷에서 자동으로 받아온 소스 파일이 저장되는 디렉터리
GO111MODULE
- on : 빌드 중에 GOPATH 대신 모듈에 있는 패키지 사용
- off : 빌드 중에 GOPATH에 있는 패키지 사용
- auto(default) : 현재 디렉토리가 GOPATH 외부에 있고 go.mod 파일이 포함된 경우 모듈을 사용, go.mod가 없는 경우 GOPATH 패키지 사용
예제 실행
package main
import "fmt"
func main() {
fmt.Println("hello world!")
}
go run
go 파일을 실행시키기 위한 명령어
go run .\helloworld.go
go run .\helloworld.go
# hello world!
hello world!
go build
go 파일의 실행 파일을 만들기 위한 명령어 (.exe)
go build .\helloworld.go
# helloworld.exe 파일 생성
go install
현재 속해있는 디렉토리 코드 전체에 대한 실행 파일을 생성.
go install helloworld.go
# $GOPATH/bin 하위 실행 파일 생성 확인
go test
// calc.go
package calc
func Sum(a ...int) int {
sum := 0
for _, i := range a {
sum += i
}
return sum
}
// calc_test.go
package calc_test
import (
"calc"
"testing"
)
func TestSum(t *testing.T) {
s := calc.Sum(1, 2, 3)
if s != 6 {
t.Error("Wrong result")
}
}
go 테스트 코드 실행
go test
패키지 단위로 실행
1. *_test.go 파일로 생성
2. "testing" 표준 패키지 import
3. TestXxx 메서드로 사용
go.mod
module 생성
# go mod init github.com/todo-golang
go mod init [module 명]
모듈을 정의하고 종속성 정보를 저장하고 있는 파일
module github.com/search-api
go 1.18
require (
github.com/gin-gonic/gin v1.8.1
github.com/mitchellh/mapstructure v1.5.0
...
)
- module
모듈 경로 지정
- require
빌드시 필요한 종속성 정보 저장
- replace
모듈의 특정 버전이나 버전 전체를 대체할 떄 사용, => 를 통해 우측에 설정된 패키지 버전으로 대체
replace example.com/some/dependency => example.com/some/dependency v1.2.3
- exclude
특정 버전을 사용하지 않도록 할 떄 사용
exclude example.com/some/dependency
go.sum
go.mod에 종속성 정보가 추가될 떄 생성
'Go' 카테고리의 다른 글
Elasticsearch Golang Client 사용하기 (0) | 2023.08.25 |
---|---|
Golang RabbitMQ 클라이언트 사용해보기 (0) | 2022.08.19 |
Go Gin 사용해보기 (0) | 2022.08.16 |