1// run 2 3// Copyright 2017 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7package main 8 9import ( 10 "encoding/binary" 11) 12 13var ( 14 ch1 = make(chan int) 15 ch2 = make(chan int) 16 17 bin = []byte("a\000\000\001") 18 want = binary.BigEndian.Uint32(bin) 19 20 c consumer = noopConsumer{} 21) 22 23type msg struct { 24 code uint32 25} 26 27type consumer interface { 28 consume(msg) 29} 30 31type noopConsumer struct{} 32 33func (noopConsumer) consume(msg) {} 34 35func init() { 36 close(ch1) 37} 38 39func main() { 40 var m msg 41 m.code = binary.BigEndian.Uint32(bin) 42 43 select { 44 case <-ch1: 45 c.consume(m) 46 if m.code != want { 47 // can not use m.code here, or it will work 48 panic("BigEndian read failed") 49 } 50 case <-ch2: 51 } 52} 53