Functions
Goμ functionsλ₯Ό μμ보μμ
Types in fuction
ν¨μμ argumentμ return κ°μ typeμ λͺ μν΄μ€μΌ νλ€
compilerμκ² typeμ΄ λ¬΄μμΈμ§ μλ €μ€μΌλ‘μ¨ λΉ λ₯΄κ² compile ν μ μκ² νλ€!
Javaλ μ μ¬! Pythonμ΄λμ λ무 λ€λ¦...
ex)
argument λ€μ typeμ΄ κ°μΌλ©΄ ν λ²λ§ λͺ μν΄λ λλ€
ex)
returnμ΄ νμν ν¨μλ κΌ return typeμ λͺ μν΄μΌ νλ€!
Multiple Return Values
Goλ multiple return value λ₯Ό μ§μνλ€
λ§μ νλ‘κ·Έλλ° μΈμ΄λ€κ³Ό λ€λ₯Έ Goμ νΉμ§μ΄λ€!
ex)
λ¨, return νλ κ°μλ³΄λ€ μ κ² κ°μ λ°μ μ μλ€!
ex) Errorκ° λ°μν μμ
lenAndUpperλ λ κ°μ κ°μ return νκΈ° λλ¬Έμ μλμ κ°μ errorλ₯Ό λ±λλ€
μ΄λ΄ λμλ
_
λ₯Ό λ£μ΄ value κ°μ 무μνλλ‘ νλ©΄ λλ€ex)
μ΄λ κ²
underscore (_)
λ ignored valueλ‘ μ¬μ©ν μ μλ€!compilerλ ignored valueλ₯Ό λ§κ·Έλλ‘ λ¬΄μνλ€
Variadic functions
...
μ μ¬μ©νμ¬ κ°λ³μΈμ ν¨μλ₯Ό λ§λ€ μ μλ€λ°©λ²μ typeμμ
...
λ₯Ό λΆμ΄λ©΄ λλ€! μ΄κ² λμ΄λ€!ex)
Naked returns
ν¨μμ return typeμ λͺ μν λ returnν κ°μ 미리 μ μΈνκ³ , λΉμ΄μλ returnμ νλ κ²
μ μΈ ν λ μ΄κΈ°ν λλ―λ‘ ν¨μ λ΄λΆμμλ ν΄λΉ variableμ update νλ κ²μ΄λ€!
returnν valueλ₯Ό λͺ μνμ§ μκ³ return ν μ μλ€
ex)
νμ€νκ² μ΄λ€ κ°μ΄ return λλμ§ λͺ μνκ³ μΆμΌλ©΄ μ¬μ©νμ§ μλ κ²μ΄ μ’λ€
defer
functionμ΄ λλ ν μ€νλλ μ½λ
ν¨μκ° μ’ λ£λμμ λ deferλ₯Ό ν΅ν΄ μΆκ°μ μΌλ‘ 무μΈκ° λμν μ μλλ‘ μ€μ ν μ μλ€
return κ°μ΄ μλ ν¨μλΌλ©΄ return λ νμ defer κ° μ€νλλ€
ex)
deferλ₯Ό ν¨μ μ’ λ£ ν imageλ fileμ λ«κ±°λ μμ νκ³ , API request 보λ΄λ κ²μ νμ©ν μ μλ€!
Last updated
Was this helpful?