Study/Go

Linux GoLang 설치하는 방법

AC 2022. 7. 11. 11:25

OS - Linux Mint

 

1. GoLang 내려받기

(wget 을 이용해서 내려받기 또는 브라우저를 이용해서 내려받는 방법 두 가지 방법이 있어요.)

두가지 방법 중 하나를 선택해서 받으시면 되요~!

 

- wget 을 이용해서 내려받기

리눅스 CUI 터미널 창에서 wget 명령어를 이용하세요!

 

wget https://golang.org/dl/go1.x.x.linux-amd64.tar.gz

wget  https://golang.org/dl/go1.x.x.linux-amd64.tar.gz

 

- GoLang 공식 사이트에서 내려받기

www.golang.org/dl/  

 

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

golang.org

 

 

 

2. 내려받은 파일 압축 풀기

받은 압축파일을 /usr/local 위치에 풀어주도록 합니다.

 

sudo tar -C /usr/local -xvf go1.x.x.linux-amd64.tar.gz

sudo  tar -C / usr /local - xvf   go1.x.x.linux-amd64.tar.gz

 

 

 

3. GOLANG 환경변수 등록

환경변수에 PATH를 입력하세요.

(centos 경우 ~/.profile --> ~/.bash_profile 로 모두 변경 하시고 진행하세요.)

echo "export GOROOT=/usr/local/go" >> ~/.profile
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile

환경변수 등록

 

 

 

4. GOPATH(패키지 설치 경로) 환경변수 등록

4.1 GOPATH 설정

(centos 경우 ~/.profile --> ~/.bash_profile 로 모두 변경 하시고 진행하세요.)

mkdir -p ~/gopath
echo "export GOPATH=$HOME/gopath" >> ~/.profile
source ~/.profile
echo $GOPATH

GOPATH 등록

 

4.2 GOPATH 설정 확인

go get 은 go패키지를 설치하는 명령어입니다. GOPATH 확인을 위해 아래 패키지를 설치할게요.

go get golang.org/x/tools/cmd/...

go get golang.org/x/tools/cmd /...

 

입력하시고 설치가 끝난 후에 GOPATH 디렉터리에 들어가 보시면 위에서 받은 패키지가 설치되면서 생긴 디렉터리들을 확인하실 수 있습니다!

 

설치가 안될 경우

go env -w GO111MODULE=auto

GOPATH 확인

 

 

 

 

 

 

 

 

마지막으로 GO로 동작하는 helloworld 를 찍는 테스트 코드를 만들고 실행시켜보고 마무리할게요.

 

아래와 같은 내용으로 hello.go 파일을 만들어주세요.

package main
import "fmt"
func main() {
  fmt.Println("Hello, World")
}

 

그리고 아래 커맨드로 실행해보시면 Hello, World 가 잘 찍히는 걸 확인할 수 있어요.

 

LIST