// Function overload test
func Area(c Circle) float64 {
return math.Pi * c.radios * c.radios
}
func Area(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 name
func (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.141592653589793 6
type Address struct {
city, province string
}
type Employee struct {
name string
age int
salary float64
Address
}
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 Qingdao
The new address: Qingdao Shandong
{Jack Chen 62 6700.89 {Qingdao Shandong}}
*/
package main
import (
"fmt"
)
type rectangle struct {
length int
width int
}
func area(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))
}
func main() {
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
}
package main
import "fmt"
type myInt int
func (a myInt) add(b myInt) myInt {
return a + b
}
func main() {
num1 := myInt(5)
num2 := myInt(10)
sum := num1.add(num2)
fmt.Println("Sum is", sum)
}