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

请教一个 Go 的语法问题

  •  
  •   chaleaoch · 2020-07-22 23:22:17 +08:00 · 1943 次点击
    这是一个创建于 1372 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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-07-22 23:28:46 +08:00
    用匿名 struct 感觉为啥这么别扭
    chaleaoch
        2
    chaleaoch  
    OP
       2020-07-22 23:32:18 +08:00
    @tikazyq 我知道这是匿名, 匿名没问题,不过为什么"io/"不见连.
    Reficul
        3
    Reficul  
       2020-07-23 00:13:00 +08:00   ❤️ 1
    结构体嵌套了匿名结构 /接口的时候,引用起来就是类型名。
    CoolSpring
        4
    CoolSpring  
       2020-07-23 00:22:04 +08:00 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-07-23 00:52:44 +08:00   ❤️ 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-07-23 00:53:51 +08:00   ❤️ 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-07-23 14:52:29 +08:00
    建议过一遍入门教程.
    chaleaoch
        8
    chaleaoch  
    OP
       2020-07-25 11:10:12 +08:00
    @byzf 谢谢 请问那本入门教程, 我看过 GO 语言圣经和 GO 高级编程 两本教材 书中的例子 没有类似的.
    chaleaoch
        9
    chaleaoch  
    OP
       2020-07-25 11:11:22 +08:00
    @byzf 我说错了,这个例子就是 go 高级编程中的例子,但是 他没有说明为什么. Go 语言圣经里面没有类似例子.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   966 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:29 · PVG 05:29 · LAX 14:29 · JFK 17:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.