英文:
Assign new values later to the underlying object of the interface in go
问题
我正在尝试在下面的代码中为接口的底层结构分配新值。但它保留了旧值。以下是示例代码。
package main
import (
"fmt"
"math"
)
type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
Name string
}
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
type Rectangle struct {
Length float64
Width float64
Name string
}
func (r Rectangle) Area() float64 {
return r.Length * r.Width
}
func assignRadius(s Shape, radius float64, name string) {
switch s := s.(type) {
case Circle:
s.Radius = radius
s.Name = name
case Rectangle:
s.Length = radius
s.Name = name
}
}
func main() {
var s Shape
c := Circle{Radius: 0, Name: "My Circle"}
s = c
fmt.Println(s.Area())
fmt.Println(c.Radius)
fmt.Println(c.Name)
assignRadius(s, 10, "My New Circle")
fmt.Println(c.Radius)
fmt.Println(c.Name)
}
在assignRadius
函数中,Shape
的类型事先是未知的。我知道这与指针有关,但无法弄清楚。
英文:
I am trying to assign new values to the underlying structure of an interface in code below. But it keeps the older values. Below is the example code.
package main
import (
"fmt"
"math"
)
type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
Name string
}
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
type Rectangle struct {
Length float64
Width float64
Name string
}
func (r Rectangle) Area() float64 {
return r.Length * r.Width
}
func assignRadius(s Shape, radius float64, name string) {
switch s := s.(type) {
case Circle:
s.Radius = radius
s.Name = name
case Rectangle:
s.Length = radius
s.Name = name
}
}
func main() {
var s Shape
c := Circle{Radius: 0, Name: "My Circle"}
s = c
fmt.Println(s.Area())
fmt.Println(c.Radius)
fmt.Println(c.Name)
assignRadius(s, 10, "My New Circle")
fmt.Println(c.Radius)
fmt.Println(c.Name)
}
The type of Shape
isn't known beforehand in the assignRadius
. I know it has something to do with the pointers. But can't figure it out.
答案1
得分: 2
接口变量s
包含了形状值的一个副本。要像你尝试的那样修改它,它必须包含指向该形状的指针:
var s Shape
c := Circle{Radius: 0, Name: "My Circle"}
s = &c
在修改它们的函数中,你必须对指针值进行类型断言:
func assignRadius(s Shape, radius float64, name string) {
switch s := s.(type) {
case *Circle:
s.Radius = radius
s.Name = name
case *Rectangle:
s.Length = radius
s.Name = name
}
}
英文:
The interface variable s
contains a copy of the shape value. To modify it like you attempted to do, it has to contain a pointer to that shape:
var s Shape
c := Circle{Radius: 0, Name: "My Circle"}
s = &c
and in the function modifying them, you have to type-assert the pointer value:
func assignRadius(s Shape, radius float64, name string) {
switch s := s.(type) {
case *Circle:
s.Radius = radius
s.Name = name
case *Rectangle:
s.Length = radius
s.Name = name
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论