본문 바로가기
Go

THEORY - for (feat. range, ...args)

by HMangoo 2022. 2. 9.
// Loop : only "for"

package main

import "fmt"

func Add(numbers ...int) int {
    total := 0
    for _, number := range numbers {
        // index는 현재 쓸 일이 없으므로 ignored value(_)로 처리
        total += number
    }
    return total
    /*
        range
            - array에 loop를 적용할 수 있게함
            - range는 index를 줌
    */
}

func main() {
    result := Add(1,2,3,4,5,6,7,8,9)
    fmt.Println(result)
}

'Go' 카테고리의 다른 글

THEORY - Pointer  (0) 2022.02.09
THEORY - If & switch  (0) 2022.02.09
THEORY - Function2  (0) 2022.02.09
THEORY - Function1  (0) 2022.02.09
THEORY - Variables & Constants  (0) 2022.02.09

댓글