mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-24 23:29:01 +00:00
31 lines
786 B
Go
31 lines
786 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 77.
|
|
|
|
// Netflag demonstrates an integer type used as a bit field.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
. "net"
|
|
)
|
|
|
|
//!+
|
|
func IsUp(v Flags) bool { return v&FlagUp == FlagUp }
|
|
func TurnDown(v *Flags) { *v &^= FlagUp }
|
|
func SetBroadcast(v *Flags) { *v |= FlagBroadcast }
|
|
func IsCast(v Flags) bool { return v&(FlagBroadcast|FlagMulticast) != 0 }
|
|
|
|
func main() {
|
|
var v Flags = FlagMulticast | FlagUp
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10001 true"
|
|
TurnDown(&v)
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10000 false"
|
|
SetBroadcast(&v)
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10010 false"
|
|
fmt.Printf("%b %t\n", v, IsCast(v)) // "10010 true"
|
|
}
|
|
|
|
//!-
|