본문 바로가기
Go

THEORY - Function1

by HMangoo 2022. 2. 9.
package main

import (
    "fmt"
    "strings"
)

/*
    Go : typescript
    => function을 생성할 때, parameter와 return의 type을 항상 명시해주어야 함
*/
func multiply(a int, b int) int {
    return a * b
}

// parameter가 모두 int인 경우 아래와 같이 작성할 수 있다.
func plus(a, b int) int {
    return a + b
}

/*
    Go function의 awesome한 특징은 함수들이 여러 개의 return을 가질 수 있다는 것
*/
func lenAndUpper(name string) (int, string) {
    return len(name), strings.ToUpper(name)
}

// infinite argument
func repeatMe(words ...string) {
    fmt.Println(words)
}

func main() {
    fmt.Println(multiply(2,2))
    fmt.Println(plus(2,2))

    totalLength, upperName := lenAndUpper("mangoo")
    // totalLength, _ := lenAndUpper("mangoo") - _ : ignored value
    fmt.Println(totalLength, upperName)
    // 항상 fmt.Println를 해주는 이유는 Go가 무엇인가를 만들고 사용하지 않는다면 error를 발생시키기 때문

    repeatMe("a", "b", "c", "d", "e", "f", "g")

}

'Go' 카테고리의 다른 글

THEORY - If & switch  (0) 2022.02.09
THEORY - for (feat. range, ...args)  (0) 2022.02.09
THEORY - Function2  (0) 2022.02.09
THEORY - Variables & Constants  (0) 2022.02.09
THEORY - Main Package & Import  (0) 2022.02.09

댓글