🍳머리말
go 설치 및 환경변수 설정 설명글입니다.
📕환경
📔 window 10
window + r을 눌러 winver를 치고 확인을 누르면 version을 확인할 수 있습니다
📕 go 설치
📔 설명
go는 여기서 설치할 수 있습니다.
Downloads - The Go Programming Language
Downloads After downloading a binary release suitable for your system, please follow the installation instructions. If you are building from source, follow the source installation instructions. See the release history for more information about Go releases
go.dev
접속 후 아래로 내려보시면 window용 msi 설치 file을 download할 수 있습니다.
fownload file을 실행해 go를 설치할 경로를 지정하고 next를 적절히 눌러주면 설치됩니다.
저는 \C:\Program Files에 실행가능한 go를 설치하도록 설정했습니다.
📕 환경설정
귀찮게도 window는 이런 실행 file을 download받았을 때 환경변수를 따로 설정해줘야 합니다. 이를 시스템 변수라고 칭하고 방금 download 받은 경로는 저장해야합니다.
GOROOT와 GOPATH라는 변수명으로 필요한 경로를 지정한 후 저장합니다.
GOROOT는 Go가 저장된 folder 경로를 값으로 가집니다.
GOPATH는 사용자가 project를 만들어 사용할 folder로 GOROOT와는 다른 경로로 지정되어야 합니다.
📔 GOROOT
설치 경로는 defulat로 C:\Go\ 가 됩니다. 제 경우에는 C:\Program Files\Go에 설치했습니다. 이제 경로에 주의해 환경 변수 등록을 해줍니다.
https://stackoverflow.com/questions/7970390/what-should-be-the-values-of-gopath-and-goroot
What should be the values of GOPATH and GOROOT?
I'm trying to install doozer like this: $ goinstall github.com/ha/doozer I get these errors. goinstall: os: go/build: package could not be found locally goinstall: fmt: go/build: package could ...
stackoverflow.com
시스템 변수의 새로 만들기 button을 눌러 추가해줍니다.
설치되면 Path에 go가 설치된 경로가 추가되나 혹시 없다면, Path에 go bin을 넣어줘야 합니다.
앞으로 go file을 build, compile 실행할 때 해당 경로를 통해 실행하게 됩니다.
📔 GOPATH
GOPATH를 만들기 전에 folder가 제대로 생성되어 있는지 확인해줍니다.
주로 C:\user\{user명}\하위에 go folder 내 bin pkg src folder가 존재해야합니다.
없으면 이런 구성이 되도록 만들어주면 됩니다.
이후 다시 환경변수로 돌아가 GOPATH를 추가해줍니다. user명은 가렸습니다.
📔 확인
terminal을 켜서 go version을 입력해 시스템 변수를 인식하는지 봐줍니다.
go env를 입력해 GOPATH와 GOROOT가 구분되어 제대로 설정됐는지 확인해줍니다.
GOROOT와 GOPATH경로가 같다면 bash 같은 terminal을 실행해 변수를 편집해줘야 합니다.
export GOROOT=C:\Program Files\Go\
기본 cmd로는 set명령어를 사용합니다.
set GOROOT="C:\Program Files\Go\"
📕 예제 작성
📔 main.go 작성
원하는 작업 folder 하위에 main.go file을 생성해 다음 code를 넣어줍니다.
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
이제 go run ./main.go 명령어를 실행해 결과를 확인합니다.
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Go' 카테고리의 다른 글
(Go error) - missing go.sum entry for module providing package <package_name> (0) | 2022.08.23 |
---|---|
(client-go Error) - "but does not contain package k8s.io/api/auditregistration/v1alpha1" (0) | 2022.08.12 |
(Go) - go get과 go mod download 차이 (0) | 2022.08.11 |
(Go) - Marshal, Unmarshal 함수 (0) | 2022.05.10 |
(Go) - test하기 (0) | 2022.05.03 |