상수정의시 첫번째 상수변수에 지정하면 자동으로 숫자를 배정한다.
const (
CategoryBooks = iota // 0
CategoryHealth // 1
CategoryClothing // 2
)
package main
import (
"fmt"
)
"fmt"
)
type AudioOutput int
const (
OutMute AudioOutput = iota // 0
OutMono // 1
OutStereo // 2
_ // 3과 4는 무시한다.
_
OutSurround // 5
)
OutMute AudioOutput = iota // 0
OutMono // 1
OutStereo // 2
_ // 3과 4는 무시한다.
_
OutSurround // 5
)
// 비트 연산자를 이용한 할당.
//<<는 숫자를 하나씩 좌측으로 이동한다.
type Allergen int
const (
IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001
IgChocolate // 1 << 1 which is 00000010
IgNuts // 1 << 2 which is 00000100
IgStrawberries // 1 << 3 which is 00001000
IgShellfish // 1 << 4 which is 00010000
)
IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001
IgChocolate // 1 << 1 which is 00000010
IgNuts // 1 << 2 which is 00000100
IgStrawberries // 1 << 3 which is 00001000
IgShellfish // 1 << 4 which is 00010000
)
func main() {
fmt.Println(" OutMute ", OutMute)
fmt.Println(" OutSurround ", OutSurround)
fmt.Println(" IgNuts ", IgNuts )
fmt.Println(" OutSurround ", OutSurround)
fmt.Println(" IgNuts ", IgNuts )
}
// 결과값
OutMute 0
OutSurround 5
IgNuts 4
OutSurround 5
IgNuts 4
댓글
댓글 쓰기