Improved methods exercise with custom types
This commit is contained in:
parent
84149fc850
commit
baa3b77552
|
@ -2,17 +2,33 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Nums struct {
|
type Person struct {
|
||||||
X float64
|
name string
|
||||||
|
country string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n Nums) Multiply() float64 {
|
type yearType int
|
||||||
return n.X * 2
|
|
||||||
|
func (p Person) Create() (newP *Person) {
|
||||||
|
var newPerson = new(Person)
|
||||||
|
newPerson.name = "John"
|
||||||
|
newPerson.country = "Spain"
|
||||||
|
return newPerson
|
||||||
|
}
|
||||||
|
|
||||||
|
func (y yearType) currentYear() (year yearType) {
|
||||||
|
t := time.Now()
|
||||||
|
ye := t.Year()
|
||||||
|
yea := yearType(ye)
|
||||||
|
return yea
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
newnumber := Nums{10}
|
p := Person{}
|
||||||
fmt.Println(newnumber.Multiply())
|
var ourYear yearType
|
||||||
|
newP := p.Create()
|
||||||
|
fmt.Printf("Hello, I am %s and live in %s and the current year is %d\n", newP.name, newP.country, ourYear.currentYear())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue