golang中值类型/指针类型的变量区别的相关疑问

Tak00 · 2018-07-13 20:37:29 · 740 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2018-07-13 20:37:29 的主题,其中的信息可能已经有所发展或是发生改变。

type T struct {
    Name string
}
func (t T) M1 () {
    t.Name = "name1"
}
func (t *T) M2 () {
    t.Name = "name2"
}
type intf interface {
    M1()
    M2()
}
func main() {
    var t1 T = T{"t1"}
    t1.M1()
    t1.M2()
    var t2 intf = t1
    t2.M1()
    t2.M2()
}

为什么当我使用T类型的变量时,赋值给intf接口的时候,会出错,但是我使用var tf intf = &t1的时候就是正确的 按理说,如果说var tf intf = t1会出错,那么就说明t1,也就是T类型没有实现M2的方法,那么同理&t1,也就是T类型的指针,也没有实现M1的方法呀,但是为什么使用指针就能够赋值给接口~~~


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

740 次点击  
加入收藏 微博
6 回复  |  直到 2018-07-16 10:08:30
jdxj
jdxj · #1 · 7年之前
  • 通过reflect的测试结果来看,*T确实实现了M1方法,测试代码如下: ``` package main

import ( "fmt" "reflect" )

type intf interface { M1() M2() }

type T struct { Name string }

func(t T) M1() { t.Name = "name1" }

func(t *T) M2() { t.Name = "name2" }

func main() { var t1 T = T{"1"} var t2 intf = &t1

t2Type := reflect.TypeOf(t2)

numMethod := t2Type.NumMethod()
fmt.Println("the count of Method: ", numMethod)

for i := 0; i < numMethod; i++ {
    fmt.Println(i, " ", t2Type.Method(i).Name, t2Type.Method(i).Type)
}

}


- 运行结果如下:

the count of Method: 2 0 M1 func(main.T) 1 M2 func(main.T)


- 另外,如果在以上代码中,删除 `T` 实现的 `M1()` 方法,将会报如下错误:

command-line-arguments

./test.go:26:6: cannot use &t1 (type T) as type intf in assignment: T does not implement intf (missing M1 method) ```

jdxj
jdxj · #2 · 7年之前
jdxjjdxj #1 回复

- 通过reflect的测试结果来看,*T确实实现了M1方法,测试代码如下: ``` package main import ( "fmt" "reflect" ) type intf interface { M1() M2() } type T struct { Name string } func(t T) M1() { t.Name = "name1" } func(t *T) M2() { t.Name = "name2" } func main() { var t1 T = T{"1"} var t2 intf = &t1 t2Type := reflect.TypeOf(t2) numMethod := t2Type.NumMethod() fmt.Println("the count of Method: ", numMethod) for i := 0; i < numMethod; i++ { fmt.Println(i, " ", t2Type.Method(i).Name, t2Type.Method(i).Type) } } ``` - 运行结果如下: ``` the count of Method: 2 0 M1 func(*main.T) 1 M2 func(*main.T) ``` - 另外,如果在以上代码中,删除 `T` 实现的 `M1()` 方法,将会报如下错误: ``` # command-line-arguments ./test.go:26:6: cannot use &t1 (type *T) as type intf in assignment: *T does not implement intf (missing M1 method) ```

天哪,这格式乱了

pjj506406619
pjj506406619 · #3 · 7年之前

因为函数接收者接收T类型的参数时,T类型及T类型都能实现,接收T类型参数时,只有*T类型的能实现,你可以看看 go语言实战 这本书,在第五章有详细说明,或者参考go语言官方规范定义

pjj506406619
pjj506406619 · #4 · 7年之前
pjj506406619pjj506406619 #3 回复

因为函数接收者接收T类型的参数时,T类型及*T类型都能实现,接收*T类型参数时,只有*T类型的能实现,你可以看看 go语言实战 这本书,在第五章有详细说明,或者参考go语言官方规范定义

因为函数接收者接收T类型的参数时,T类型及T类型都能实现,接收T类型参数时,只有T类型的能实现,你可以看看 go语言实战 这本书,在第五章有详细说明,或者参考go语言官方规范定义

pjj506406619
pjj506406619 · #5 · 7年之前

这网站会吃字符吗,我的星号一会在,一会不在

Tak00
Tak00 · #6 · 7年之前
pjj506406619pjj506406619 #5 回复

这网站会吃字符吗,我的星号一会在,一会不在

因为函数接收者接收T类型的参数时,T类型及T的指针类型都能实现,接收T的指针类型参数时,只有T的指针类型的能实现。(是这个意思嘛~

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传