๋ฐ์ํ
[์ถ์ฒ - Nomad Coders]
naked return
function์ ๋ ๋ค๋ฅธ ๋ฉ์ง ๋ถ๋ถ์
'naked' return ์ด๋ผ ๋ถ๋ฆฌ๋ function ์ด๋ค.
package main
import (
"fmt"
"strings"
)
func lenAndUpper(name string) (length int, uppercase string) {
length = len(name)
uppercase = strings.ToUpper(name)
return // naked return
}
func main() {
totalLength, up := lenAndUpper("nico")
fmt.Println(totalLength, up)
}
์ฌ๊ธฐ์๋ Go๊ฐ ์๋์ ์ผ๋ก (length int, uppercase string) ์๋ค๋ค์ return ํ ๊ฑฐ๋ผ๊ณ ์์ ๋ฃ๋๋ค.
variable์ ์ด๋ฆ์ ๊ฐ์์ผ ํ๋ค.
return length, uppercase
์ด๋ ๊ฒ ์์ฑํด๋ ์ ์๋๋๋ค. ํ์ง๋ง ๊ตณ์ด ๊ทธ๋ด ํ์๊ฐ ์๋ค๋ ๋ป์ด๋ค.
defer
defer์ function์ด ๊ฐ์ return ํ๊ณ ๋๋ฉด ์คํ๋๋ ๊ฒ์ด๋ค.
package main
import (
"fmt"
"strings"
)
func lenAndUpper(name string) (length int, uppercase string) {
defer fmt.Println("I'm done") // function์ด ๊ฐ์ return ํ๊ณ ๋๋ฉด ์คํ ๋ ๊ฒ์ด๋ค.
length = len(name)
uppercase = strings.ToUpper(name)
return
}
func main() {
totalLength, up := lenAndUpper("nico")
fmt.Println(totalLength, up)
}
๋ฐ์ํ
'Data Base > Go' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #1.6 If with a Twist (0) | 2021.10.29 |
---|---|
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #1.5 for, range, ...args (0) | 2021.10.26 |
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #1.3 Functions part One (0) | 2021.10.26 |
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #1.2 Variables and Constants (0) | 2021.10.26 |
[์ฝ๊ณ ๋น ๋ฅธ Go ์์ํ๊ธฐ] #1.0 Main Package (0) | 2021.10.26 |
๋๊ธ