CATCHV Blog

Golang cross-compile windows, mac(darwin), linux 본문

Dev/Go

Golang cross-compile windows, mac(darwin), linux

catchv 2023. 12. 18. 14:39
반응형

go에서 빌드를 하는 경우 바이너리 실행파일을 생성합니다.

 

그런데 바이너리 실행파일은 OS(windows, mac, linux)별 아키텍쳐(x86, x64, arm64)별로 다른 구조 및 라이브러리 참조가 다르기 때문에 빌드시에 어떤 타켓으로 빌드 할 지 결정이 필요합니다.

 

go build 시에는 go env에 저장된 환경변수 파일에 따른 정보로 빌드가 됩니다.

GOARCH='arm64'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOOS='darwin'

 

1. Windows 바이너리 컴파일

# Windows x64 빌드
GOOS=windows GOARCH=amd64 go build 

# Windows x86 빌드
GOOS=windows GOARCH=386 go build

 

2. Mac 바이너리 컴파일

# Mac x64 빌드
GOOS=darwin GOARCH=amd64 go build 

# Mac x86 빌드
GOOS=darwin GOARCH=386 go build

# Mac arm64 빌드(M1, M2 등)
GOOS=darwin GOARCH=arm64 go build

 

3. linux 바이너리 컴파일

# Windows x64 빌드
GOOS=linux GOARCH=amd64 go build 

# Windows x86 빌드
GOOS=linux GOARCH=386 go build

# Windows x86 빌드
GOOS=linux GOARCH=arm64 go build

 

Golang에서 지원되는 크로스 컴파일 리스트는 Go Doc을 참고 하면 됩니다.

https://go.dev/doc/install/source#environment

 

Installing Go from source - The Go Programming Language

Documentation Download and install Installing Go from source Installing Go from source This topic describes how to build and run Go from source code. To install with an installer, see Download and install. Introduction Go is an open source project, distrib

go.dev

 

 

반응형

'Dev > Go' 카테고리의 다른 글

go 1.22 for loop 변경  (0) 2024.02.23
dotnet으로 Golang gin 로컬 개발(localhost) 인증서 만들기  (0) 2024.01.11
go - package  (1) 2023.11.24
go module upgrade  (0) 2023.11.13
golang fiber + otel + recover middleware 호출 순서  (0) 2023.11.10
Comments