new一个新的对象
package main
import "fmt"
type rec struct {
width,height int
}
func (r *rec) area() int {
return r.width * r.height
}
func main() {
r := rec{width:100,height:100}
fmt.Println("area:",r.area())
newR := new(rec) // 相当于 &r
newR.width=1
newR.height=2
fmt.Println("area1",newR.area())
}
推荐文章: