go的表达式调用编译器不会进行自动转换 未完成

package main

import (
	"fmt"
)

type T struct {
	a int
}

func (t T) Get() int {
	return t.a
}
func (t *T) Set(i int) {
	t.a = i
}

//表达式`T.Get`和`(*T).Set`被称为方法表达式,
//需要注意的是在方法表达式中编译器不会做自动转换。
//值调用会自动转换,表达式调用则不会,例如:
type Data struct{}

func (d Data) TestValue() {
	fmt.Println("TestValue")
}

func (d *Data) TestPointer() {
	fmt.Println("TestPointer")
}

//声明一个类型变量a
var a Data = struct{}{}
var  Data1 = struct{}

func main() {
	//表达式调用编译器不会进行自动转换
	a.TestValue()
	//Data1.TestValue()
	//Data.TestValue(&a)
	(*Data).TestPointer(&a)
	//Data.TestPointer(&a) //type Data has no method TestPointer
	//值调用编译器会进行自动转换

	//y: = (&a).TestValue //编译器帮助转换a.TestValue
	//g: = a.TestPointer  //会转换为(&a).TestPointer
	//fmt.Println(y,g)
}
posted @ 2023-03-04 23:46  ty1539  阅读(11)  评论(0编辑  收藏  举报