23 September 2017

Table of Contents

方法

type Vertex struct {
        X float64
        Y float64
}

func (v Vertex) print() {
        fmt.Printf("hello world\n")
}

func (v Vertex) printValue(value int) {
        fmt.Printf("%d\n", value)
}

func main() {
        v := Vertex{3, 4}
        v.print()
        v.printValue(5)
}
type MyFloat float64

func (f MyFloat) Abs() float64 {
        if f < 0 {
                return float64(-f)
        }
        return float64(f)
}

func main() {
        f := MyFloat(-math.Sqrt2)
        fmt.Println(f.Abs())
}
type Vertex struct {
        X float64
        Y float64
}

func (v Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func (v Vertex) Scale(f float64) {
        v.X = v.X * f
        v.Y = v.Y * f
}

func main() {
        v := Vertex{3, 4}
        v.Scale(10)
        fmt.Println(v.Abs())    // 5
}
type Vertex struct {
        X float64
        Y float64
}

func (v Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func (v *Vertex) Scale(f float64) {
        v.X = v.X * f
        v.Y = v.Y * f
}

func main() {
        v := Vertex{3, 4}
        v.Scale(10)
        fmt.Println(v.Abs())    // 50
}

继承

type Human struct {
        name  string
        age   int
        phone string
}

type Student struct {
        Human  // 匿名字段
        school string
}

type Employee struct {
        Human          // 匿名字段
        name    string // name 字段覆盖
        company string
}

//在 human 上面定义了一个 method
func (h *Human) SayHi() {
        fmt.Printf("%s\n", h.name)
}

func (e *Employee) SayHi(arg string) {
        pp.Println(e.name + " " + arg)
}

func main() {
        mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}
        sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "name", "Golang Inc"}

        mark.SayHi()
        sam.SayHi("ok")
}

接口

type Abser interface {
        Abs() float64
}

type Vertex struct {
        X float64
        Y float64
}

func (v *Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
}



type MyFloat float64

func (f MyFloat) Abs() float64 {
        if f < 0 {
                return float64(-f)
        }
        return float64(f)
}

func main() {
var a Abser
        f := MyFloat(-math.Sqrt2)
        v := Vertex{3, 4}

        a = f     // a MyFloat 实现了 Abser
        fmt.Println(a.Abs())
        a = &v    // a *Vertex 实现了 Abser

        // 下面一行,v 是一个 Vertex(而不是 *Vertex)
        // 所以没有实现 Abser。
        // a = v

        fmt.Println(a.Abs())
}
type I interface {
        M()
}

type T struct {
        S string
}

func (t *T) M() {
        fmt.Println(t.S)
}



type F float64

func (f F) M() {
        fmt.Println(f)
}

func main() {
        var i I

        i = &T{"Hello"}
        i.M()

        i = F(math.Pi)
        i.M()
}

类型

func do(i interface{}) {
        value, ok := i.(int)

        pp.Println(value)
        pp.Println(ok)
}

func main() {
        do(11)   // 11, true
        do("11") // 0, false
}




func do(i interface{}) {
        switch v := i.(type) {
        case int:
                fmt.Printf("Twice %v is %v\n", v, v*2)
        case string:
                fmt.Printf("%q is %v bytes long\n", v, len(v))
        default:
                fmt.Printf("I don't know about type %T!\n", v)
        }
}

func main() {
        do(21)
        do("hello")
        do(true)
}

Stringer

type Person struct {
      Name string
      Age  int
}

func (p Person) String() string {
      return fmt.Sprintf("%v (%v years)", p.Name, p.Age)
}

func main() {
      a := Person{"Arthur Dent", 42}
      z := Person{"Zaphod Beeblebrox", 9001}
      fmt.Println(a, z)
}
type IPAddr [4]byte

func (ip IPAddr) String () string {
        str := ""
        for i, s := range ip {
                if i == 0 {
                        str += strconv.Itoa(int(s))
                } else {
                        str += "." + strconv.Itoa(int(s))
                }
        }

        return str
}

func main() {
        hosts := map[string]IPAddr{
                "loopback":  {127, 0, 0, 1},
                "googleDNS": {8, 8, 8, 8},
        }
        for name, ip := range hosts {
                fmt.Printf("%v: %v\n", name, ip)
        }
}

misc

from: http://1234n.com/?post/rackdp

package main

type IServer interface {
      Start()
      Stop()
}

type IRestartAble interface {
      Restart()
}

//--------------------------------------------

type ServerTypeA struct {
}

func (s *ServerTypeA) Start() {
      println("ServerTypeA start")
}

func (s *ServerTypeA) Stop() {
      println("ServerTypeA stop")
}

func (s *ServerTypeA) Restart() {
      println("ServerTypeA restart")
}

//--------------------------------------------

type ServerTypeB struct {
}

func (s *ServerTypeB) Start() {
      println("ServerTypeB start")
}

func (s *ServerTypeB) Stop() {
      println("ServerTypeB stop")
}


func main() {
      var sa IServer = new(ServerTypeA)
      sa.Start()
      sa.Stop()

      s1, ok1 := sa.(IRestartAble)

      if ok1 {
              s1.Restart()
      } else {
              println("ServerTypeA not restart able.")
      }

      var sb IServer = new(ServerTypeB)
      sb.Start()
      sb.Stop()

      s2, ok2 := sb.(IRestartAble)

      if ok2 {
              s2.Restart()
      } else {
              println("ServerTypeB not restart able.")
      }
}