1
0
Fork 0
This repository has been archived on 2023-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
lesson-go_problems/1-pointers-in-go/main.go
2020-11-17 07:48:34 +05:00

55 lines
1.3 KiB
Go

package main
import (
"fmt"
"time"
)
type Person struct {
firstName string
lastName string
birthday time.Time
}
func main() {
birthday, _ := time.Parse("2006-Jan-02", "1994-Aug-10")
person := Person{
firstName: "Alex",
lastName: "Kotov",
birthday: birthday,
}
printPersonByValue(person)
printPersonByRefSafe(&person)
printPersonByRefUnsafe(&person)
printPersonByRefSafe(nil)
printPersonByRefUnsafe(nil)
}
func printPersonByValue(person Person) {
fmt.Println("=== by value =====================================")
fmt.Println("First name:", person.firstName)
fmt.Println("Last name: ", person.lastName)
fmt.Println("Birthday: ", person.birthday)
}
func printPersonByRefSafe(person *Person) {
fmt.Println("=== by reference (safe) ==========================")
if person == nil {
fmt.Println("nil pointer, skip...")
return
}
fmt.Println("First name:", person.firstName)
fmt.Println("Last name: ", person.lastName)
fmt.Println("Birthday: ", person.birthday)
}
func printPersonByRefUnsafe(person *Person) {
fmt.Println("=== by reference (unsafe) ========================")
fmt.Println("First name:", person.firstName)
fmt.Println("Last name: ", person.lastName)
fmt.Println("Birthday: ", person.birthday)
}