这是结构。它实现(使用BiOperators作为方法接收器)。这些只是一些用于测试的代码,我在这里省略了它。BiOperatorsMyInterface1
type BiOperators struct {
oprt1 float64
oprt2 float64
}
我的问题是为什么我不能在类型断言后将值分配给oprt1
func testTypeAssertion() {
bio := BiOperators{111, 222}
arr := []MyInterface1{bio, &bio}
// I can do the conversion and output field oprt2
fmt.Println((arr[0].(BiOperators)).oprt2)
// But when I want to directly assign value to it, I got the following error
// cannot assign to arr[0].(BiOperators).oprt2go
// (arr[0].(BiOperators)).oprt2 = 99 <----this line is error
// and I can't create a pointer to do the assignment
// the following line has error : cannot take the address of arr[0].(BiOperators)go
// bioCvt2 := &(arr[0].(BiOperators)) <----this line is error too
// I can using the following lines to do the assignment. But it will create a new struct
bioCvt := (arr[0].(BiOperators))
bioCvt.oprt2 = 333
fmt.Println("bioCvt", bioCvt)
fmt.Println("arr[0](assigned using bio)", arr[0])
}
那么,是否无论如何都要不要在类型断言之后创建新的结构来执行字段分配?
白衣非少年
相关分类