Skip to content
jaeseok.an edited this page Apr 12, 2019 · 4 revisions

Channel

  • bidirectionnal channel 은 unidirectional channel로 convert가능
    func f(x <-chan int) int {         
           return n    
    }     
    func main() {    
          c make(chan int)       
          n := f(c)    
    }
    
  • closed channel
    • read
      x, ok <- mychan    
      if !ok {    
          return false   
      }   
      
    • write는 panic발생

Bool encoding/decoding

func main() {
	buf := new(bytes.Buffer)
	var x bool = true
	err := binary.Write(buf, binary.LittleEndian, x)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	fmt.Printf("% x \n", buf.Bytes())
	mybytes := buf.Bytes()

	//binary to bool
	var y bool = false
	bufreader := bytes.NewReader(mybytes)
	err = binary.Read(bufreader, binary.LittleEndian, &y)
	if err != nil {
		fmt.Println("binary.Read failed:", err)
	}
	fmt.Printf("read bool + %v \n", y)
}

test

Clone this wiki locally