xxxxxxxxxx
package main
import "fmt"
type Rectangle struct {
Width int
Height int
}
func (r *Rectangle) Area() int {
return r.Width * r.Height
}
func main() {
var rect *Rectangle // default value is nil
// replace this
// fmt.Println(rect.Area()) // this will panic
// with this
if rect != nil {
fmt.Println(rect.Area())
} else {
fmt.Println("rect is nil!")
}
}