Here is the struct BiOperators
. It implements MyInterface1
(using BiOperators as method receiver). These are just some code for testing and I omit it here.
type BiOperators struct {
oprt1 float64
oprt2 float64
}
My question is why I can't assign value to oprt1 after type assertion
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])
}
So is there anyway to NOT create a new struct after type assertion to do the field assignment?