请教一个 Go 的语法问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
chaleaoch

请教一个 Go 的语法问题

  •  
  •   chaleaoch 2020 年 7 月 22 日 2556 次点击
    这是一个创建于 2103 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package main import ( "io" "net/http" "net/rpc" "net/rpc/jsonrpc" ) type dd interface{} func main() { // rpc.RegisterName("HelloService", new(HelloService)) http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) { var conn io.ReadWriteCloser = struct { io.Writer io.ReadCloser }{ ReadCloser: r.Body, // 这里为什么不是 io.ReadCloser 这是一个 go 的语法特性,还是根据一个特性引申出的另一个特性? 叫什么? 谢谢 Writer: w, // dd: 3, } rpc.ServeRequest(jsonrpc.NewServerCodec(conn)) }) http.ListenAndServe(":1234", nil) } 

    问题在上端代码的注释中.提前表示感谢.

    9 条回复    2020-07-25 11:11:22 +08:00
    tikazyq
        1
    tikazyq  
       2020 年 7 月 22 日
    用匿名 struct 感觉为啥这么别扭
    chaleaoch
        2
    chaleaoch  
    OP
       2020 年 7 月 22 日
    @tikazyq 我知道这是匿名, 匿名没问题,不过为什么"io/"不见连.
    Reficul
        3
    Reficul  
       2020 年 7 月 23 日   1
    结构体嵌套了匿名结构 /接口的时候,引用起来就是类型名。
    CoolSpring
        4
    CoolSpring  
       2020 年 7 月 23 日 via Android   1
    Embedding?
    https://golang.org/doc/effective_go.html#embedding

    "If we need to refer to an embedded field directly, the type name of the field, ignoring the package qualifier, serves as a field name"
    reus
        5
    reus  
       2020 年 7 月 23 日   1
    这类问题,全都在语言规范里写了的

    https://golang.org/ref/spec#Struct_types

    A field declared with a type but no explicit field name is called an embedded field. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

    这里说了字段名是 unqualified type name,也就是不带 io.
    heimeil
        6
    heimeil  
       2020 年 7 月 23 日   1
    定义 struct 只指定类型不指定字段名,默认就会用类型名称作为字段名,不包含包名

    package main

    import (
    "fmt"
    "io"
    "reflect"
    )

    func main() {
    type T struct {
    Foo string

    io.ReadCloser
    }

    s := reflect.ValueOf(&T{}).Elem()

    for i := 0; i < s.NumField(); i++ {
    fmt.Printf("%d: %s\n", i, s.Type().Field(i).Name)
    }
    // 0: Foo
    // 1: ReadCloser
    }

    定义 struct 是定义类型,需要明确指定包名,实例化 struct 是指定字段名
    byzf
        7
    byzf  
       2020 年 7 月 23 日
    建议过一遍入门教程.
    chaleaoch
        8
    chaleaoch  
    OP
       2020 年 7 月 25 日
    @byzf 谢谢 请问那本入门教程, 我看过 GO 语言圣经和 GO 高级编程 两本教材 书中的例子 没有类似的.
    chaleaoch
        9
    chaleaoch  
    OP
       2020 年 7 月 25 日
    @byzf 我说错了,这个例子就是 go 高级编程中的例子,但是 他没有说明为什么. Go 语言圣经里面没有类似例子.
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2758 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 06:54 PVG 14:54 LAX 23:54 JFK 02:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86