Go Basics
Go λ₯Ό 곡λΆν΄ 보μμ
A huge thank you to Calvin!
What is Go Language?
Googleμ΄ κ°λ°ν programming language
λ¬Έλ²μ CμΈμ΄μ μ μ¬νμ§λ§, λμμ± (Concurrency) programmingμ λ€λ£¨κΈ° νΈνλλ‘ μ€κ³λ¨
λμμ± νλ‘κ·Έλλ°
web serviceμ κ°μ΄ DBμμ²μ΄λ Network ν΅μ κ³Ό κ°μ΄ λΉκ΅μ μκ°μ΄ λ§μ΄ 걸리λ μ°μ°μ νλ λμ νλ‘κ·Έλ¨μ΄ λ€λ₯Έ μ΄λ₯Ό λ¨Όμ ν μ μλλ‘ ν¨μ μλ―Έ
λΉκ΅μ μ΅κ·Όμ λ±μ₯ν νλ‘κ·Έλλ° μΈμ΄μ΄μ§λ§ λΉκ΅μ 볡μ‘νμ§ μκ³ μ€μ©μ μΈ μΈμ΄μ
Goλ₯Ό νμ©νλ λνμ μΈ νλ‘μ νΈλ
Docker
μKubernetes
κ° μμ
Why Go?
Goλ κ°λ¨νκ³ κ°κ²°ν μ§κ΄μ μΈ μΈμ΄ λ₯Ό μ§ν₯ν¨
OOP
λ₯Ό μ§μνλ€class, κ°μ²΄, μμμ κ°λ μ΄ μλ€
Goμμ μ°μ΄λ ν€μλλ Javaμ μ λ° μμ€μΈ 25κ° λ°μ λμ§ μμ
κ°λ¨!
Complie μΈμ΄μ§λ§ compilerμ μλκ° λ§€μ° λΉ λ₯΄κΈ° λλ¬Έμ interpreter μΈμ΄μ²λΌ μΈ μ μμ
μ’μ build system μ κ°κ³ μμ
build μλκ° λ§€μ° λΉ λ¦
λͺ μ²μ€μ μ½λλ‘ μ΄λ£¨μ΄μ§ νλ‘μ νΈλ λ¨ λͺ μ΄λ§μ complie νλ€!
Code import
κ° off url import κΈ°λ°μ΄λ€κ·Έλμ dependencyλ₯Ό κ΄λ¦¬νκΈ° μ’λ€!
Go moduleμ import μμ urlμ hash ν μ μκ² ν΄μ€λ€
λ€λ₯Έ build systemμμ .lock file μ import νλ κ²κ³Ό μ μ¬ν¨!
Install Go on Ubuntu/Mac
1. Go 1.14 μ€μΉνκΈ°
Ubuntu
longsleep/golang-backports
PPA λ₯Ό μ¬μ©νμ¬ μ€μΉ
MacOS
golang.org μμ package download & install
2. Go version νμΈνκΈ°
Ubuntu
MacOS
3. Workspace μ€μ
Go projectλ₯Ό μ§νν directoryλ₯Ό μμ±νλ€
ex)
/Users/chloe/workspace/go
ν΄λΉ directory λ΄μ μλμ κ°μ΄ directory 3κ°λ₯Ό μμ±νλ€
bin
source codeκ° compileλ ν OSλ³λ‘ μ€ν κ°λ₯ν binary fileμ΄ μ μ₯λλ κ³³
pkg
projectμ νμν packageκ° compileλμ΄ library fileλ€μ΄ μ μ₯λλ κ³³
src
μμ±ν source code λ° open source codeλ€μ μ μ₯νλ κ³³
4. environment variable μ€μ
Goλ 2κ°μ environment variableμ κ°κ³ μλ€ -
GOPATH
&GOROOT
GOROOT
Go binariesκ° μμΉν κ³³
sudo apt install golang-go
λ‘ μ€μΉνλ©΄ Go λ₯Ό default pathμ μ€μΉνλ―λ‘ λ°λ‘ μμ ν νμ μλ€!macμμλ
/usr/local/go
μ μ€μΉλλ€
GOPATH
user go envirionment path
project fileλ€μ΄ μμΉν κ³³μ μ€μ ν΄μΌ ν¨
go import
λ‘ packageλ€μ import ν λGOPATH
λ‘ λΆλ¬μ΄μΌλ°μ μΌλ‘
$HOME/go
μGOPATH
λ₯Ό μ€μ ν¨go install
μ΄λgo import
λ₯Ό ν λ go λGOPATH
λ₯Ό μ°Ύκ² λλ―λ‘ μ€μνλ€!
4-1. Ubuntu $GOPATH μ€μ
GOPATH μ€μ
PATH μ€μ
4-2. MacOS $GOPATH μ€μ
~/.zshrc
μ λ€μμ μΆκ°νλ€
+
$GOPATHμ€μ νμ go env
λͺ
λ Ήμ΄λ₯Ό νμ©νμ¬ μ λλ‘ μ μ©λμλμ§ νμΈν μ μλ€!
5. src
in $GOPATH
src
in $GOPATHGoλ downloadν codeλ€μ
$GOPATH/src
μ downloadν domainμ κΈ°μ€μΌλ‘ λΆλ₯ν΄μ μ μ₯νλ€ex) github.com, golang.org, google-golang.org, etc.
λ΄κ° μμ±ν source codeλ github.com λ΄μ github user nameμΌλ‘ directoryλ₯Ό λ§λ€μ΄μ μΆκ°ν κ²μ΄λ€
Getting started with Go
1. Main package
package μ΄λ¦μ main.go λ‘ νλ€λ κ²μ μ΄ projectλ₯Ό complie νκ³ , κ·Έκ²μ μ¬μ©ν κ²μ΄λΌλ λ»μ΄λ€
μ¦, main.go λ₯Ό μ μΈν λͺ¨λ packageλ€μ compile λμ§ μλλ€!
mainμ΄ μ§μ μ μ΄λΌμ compilerλ package μ΄λ¦μ΄ mainμΈ κ² λΆν° μ°Ύλλ€
ex)
package main
Goλ func main() {} μ΄λΌλ ν¨μλ₯Ό μ°Ύλλ€
μ¬κΈ°κ° Go programμ entrypointμ΄λ€!
μλμ μΌλ‘ compilerλ main package μ κ·Έ μμ μλ main function μ λ¨Όμ μ°Ύκ³ μ€νμν¨λ€
ex)
main.go
2. Packages and imports
Goμμ functionμ export νκ³ μΆμΌλ©΄, upper-case λ‘ μμ±νλ©΄ λλ€
ex)
something.go
main.go
μ½μ μΆλ ₯ ν¨μ - println
, print
println
, print
μΆλ ₯ ν΄λ³΄κΈ°
ex)
fmt
package import μν΄λ print / println μ μ¬μ© κ°λ₯println κ³Ό print λ line break μ 무μ μ°¨μ΄
printλ‘ μ€λ°κΏμ νλ €λ©΄ μ¬μ©νκΈ°!
Single quote μ λ¨!
Double quote μ¬μ©νκΈ°
print ν¨μλ€μ ν¨μ μμμμ μ°μ° κ²°κ³Όλ₯Ό μΆλ ₯ ν μ μμ!
ex)
fmt
package
fmt
packageμ μΆλ ₯μ μν package
μ¬μ©λ²
package main λ°μ€μ
import "fmt"
μμ±
+
Useful Go resources recommended by Calvini
gobyexample.com
tour.golang.org
blog.golang.org
dave.cheney.net
https://golang.org/doc/effective_go.html
must read!
Last updated
Was this helpful?