본문 바로가기
Go

THEORY - Function2

by HMangoo 2022. 2. 9.
package main

import (
    "fmt"
    "strings"
)

// "naked" return : return할 variable을 명시하지 않는 방법
func lenAndUpper(name string) (lenght int,uppercase string) {
    defer fmt.Println("I'm done, this is defer")
    lenght = len(name)
    uppercase = strings.ToUpper(name)
    fmt.Println("function is running")
    return
    // return lenght, uppercase를 작성하지 않아도 됨
    // <=> Go는 정의 부분을 확인하여 이 function이 (lenght int,uppercase string)을 return할 것이라 알아들음
}
/*
    defer : function이 끝났을 때 추가적으로 무엇인가 동작할 수 있음
    Go의 아주 sexy한 기능
*/

func main() {
    totalLength, up := lenAndUpper("mangoo")
    fmt.Println(totalLength, up)    
}

최종출력

'Go' 카테고리의 다른 글

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

댓글