// Function overload testfuncArea(c Circle) float64 {return math.Pi * c.radios * c.radios}funcArea(r Rectangle) int {return r.width * r.length}c := Circle {1.0}r := Rectangle {2, 3}fmt.Println(Area(c), Area(r))// # command-line-arguments// ./method.go:41:6: Area redeclared in this block// previous declaration at ./method.go:37:21// ./method.go:58:19: cannot use c (type Circle) as type Rectangle in argument to Area
// Method with same namefunc (c Circle) Area () float64 {return math.Pi * c.radios * c.radios}func (r Rectangle) Area () int {return r.length * r.width}c := Circle {1.0}r := Rectangle {2, 3}fmt.Println("Methods with same name:\n", c.Area(), r.Area())//Methods with same name:3.1415926535897936
typeAddressstruct { city, province string}typeEmployeestruct { name string age int salary float64Address}func (a *Address) changeCity (newCity string) { a.city = newCity fmt.Println("The new address:", a.city, a.province)}fmt.Println("\n\nNow emp move to Qingdao")emp1.changeCity("Qingdao")fmt.Println(emp1)/*Now emp move to QingdaoThe new address: Qingdao Shandong{Jack Chen 62 6700.89 {Qingdao Shandong}}*/
packagemainimport ( "fmt")typerectanglestruct { length int width int}funcarea(r rectangle) { fmt.Printf("Area Function result: %d\n", (r.length * r.width))}func (r rectangle) area() { fmt.Printf("Area Method result: %d\n", (r.length * r.width))}funcmain() { r :=rectangle{ length: 10, width: 5, }area(r) r.area() p :=&r/* compilation error, cannot use p (type *rectangle) as type rectangle in argument to area *///area(p) p.area()//calling value receiver with a pointer}