Created
January 23, 2023 02:10
-
-
Save reetasingh/72de55801a4e4db1911a4617868ee7e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type Circle struct { | |
r float64 | |
} | |
func (c *Circle) area() float64 { | |
if c == nil { | |
// initalize nil receiver | |
c = new(Circle) | |
c.r = 10 | |
} | |
return math.Pi * c.r * c.r | |
} | |
func main() | |
{ | |
var c *Circle | |
fmt.Println(c.area()) // returns area | |
// will throw panic for c.r | |
fmt.Println(c.r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment