Skip to content
jaeseok.an edited this page Nov 26, 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)
}

interface에 nil 대입시 nil체크 할수 없다.

type MyI interface {
    ok() string
}

type SI struct {
}

func (x *SI)ok() string {
    return "ok"
}


type Test struct {
    A string
    b MyI
}

func main() {
    var a *SI = nil
    t := Test{A: "not nil", b:a}

    if t.b != nil {
        fmt.Printf("%p", t.b)
    }
}

> "not nil" 출력

test

Clone this wiki locally