Golang

[GoLang 시작하기 2] 함수의 기본 형태, 변수와 상수

여니여니_ 2020. 3. 15. 03:01

 

 

 

함수 형태

다른 프로그래밍 언어와 비슷하게 함수를 다음과 같이 작성해서 실행할 수 있다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main
 
import (
    "fmt"
)
 
 
func main() {
    fmt.Println("Hello World")
    sayHello()
    sayBye()
}
 
func sayBye() {
    fmt.Println("Bye")
}
 
func sayHello() {
    fmt.Println("Hello")
}
 

 

변수 vs 상수

 

여느 프로그래밍 언어와 같이 변수와 상수 개념이 있다. 

 

 

변수로 선언한 name을 바꾸어도 괜찮다.

 

상수로 선언한 name을 바꾸면 오류가 발생한다.

 

 

 

한 가지 재미있는 점은 var name string="yooyeon"을 간단히 name:="yooyeon"으로 표현할 수 있다.

이렇게 축약 코드를 사용하면 Go가 string과 같은 type(자료형)을 자동으로 찾아준다. 

단 주의할 점은 축약형은 func(함수) 안에서만 적용 가능하고 변수로 정의된다.

 

변수 선언

var x []int

생성

x:=make([]float64,5)

 

다양한 자료형

bool true, false
numeric 숫자 타입
string 문자열
array 배열 []
struct 구조체 ex) type struct People{}
pointer *
function 함수, func
interface 인터페이스, interface
map map
channel 채널 chan

 

 

https://go101.org/article/basic-types-and-value-literals.html

 

Basic Types and Basic Value Literals - Go 101: an online Go programming book + knowledge base

Types can be viewed as value templates, and values can be viewed as type instances. This article will introduce the built-in basic types and their value literals in Go. Composite types will not get introduced in this article. Go supports following built-in

go101.org

 

 

 

 

패키지 검색 페이지

 

https://pkg.go.dev/

 

go.dev · go.dev

 

pkg.go.dev