Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- О iota
- Константа (const) похожа на переменную, но неизменяема
- Часто применяется группировка констант
- Ключевое слово iota может использоваться для
- автоматического присвоения значении
- const ( const (
- Online = 0 Online = iota
- Offline = 1 Offline
- Maintenance = 2 Maintenance
- Retired = 3 Retired
- ) )
- Формы:
- ------
- // Длинная: //Короткая:
- const ( const (
- L0 = iota // 0 s0 = iota //0
- L1 = iota // 1 si // 1
- L2 = iota // 2 s2 // 2
- L3 = iota // 3 s3 // 3
- L4 = iota // 4 s4 // 4
- ) )
- Пропуск значения
- ----------------
- // Пропустить значение // Начать с 3
- const ( const (
- s0 = iota // 0 i3 = iota + 3 // 3 = iota + 3
- - // 1 (пропустить) i4 // 4
- - // 2 (пропустить) i5 // 5
- s3 // 3 )
- s4 // 4
- )
- iota в паттерне перечисления
- -----------------------------
- type Direction byte
- const (
- North Direction = iota
- East
- South
- West
- )
- func (d Direction) String() string {
- switch d {
- case North:
- return fmt.Sprintf("North")
- case South:
- return fmt.Sprintf("South")
- case East:
- return fmt.Sprintf("East")
- case West:
- return fmt.Sprintf("West")
- default: return "other direction" }
- }
- Более короткий вариант:
- func (d Direction) String() string
- {
- //Создается срез, а так как Direction это byte -
- достаточно вернуть значение по индексу
- return []string{"North", "East", "South", "West"}[d]
- }
- north := North
- fmt.Pri ntln(north) // prints "North"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement