美文网首页
Struct中的函数的多种调用方式

Struct中的函数的多种调用方式

作者: bocsoft | 来源:发表于2018-11-19 17:13 被阅读0次
package main

import (
    "fmt"
    "reflect"
)

type Man struct {
    Name string
}

func(h Man) Follow(who ...string)  {
    fmt.Printf("%s just followed:\n",h.Name)
    for _,v := range who{
        fmt.Println(v)
    }
}

func main()  {
    h := &Man{"Chris"}
    fmt.Println("type of h ",reflect.TypeOf(h))//*main.Man
    fmt.Println("type of *h ",reflect.TypeOf(*h))//main.Man

    h.Follow("Aoi","Box")
    Man.Follow(*h,"Sora","BOx")

    man := Man{"A"}
    Man.Follow(man,"Hi","Super","Man")//结构体直接调用
    man.Follow("go","away")
}

相关文章

网友评论

      本文标题:Struct中的函数的多种调用方式

      本文链接:https://www.haomeiwen.com/subject/bfuafqtx.html