From 37979af331ae038f3775ba7f290d3fb0ce5ef54a Mon Sep 17 00:00:00 2001 From: Johannes Rothe Date: Wed, 9 Feb 2022 23:15:49 +0100 Subject: [PATCH] add structs --- structs/go.mod | 3 +++ structs/main.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 structs/go.mod create mode 100644 structs/main.go diff --git a/structs/go.mod b/structs/go.mod new file mode 100644 index 0000000..f2176ed --- /dev/null +++ b/structs/go.mod @@ -0,0 +1,3 @@ +module structs + +go 1.17 diff --git a/structs/main.go b/structs/main.go new file mode 100644 index 0000000..03ef969 --- /dev/null +++ b/structs/main.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +type contactInfo struct { + email string + zipCode int +} + +type person struct { + firstName string + lastName string + contactInfo +} + +func main() { + person1 := person{ + firstName: "Bert", + lastName: "Bertrandt", + contactInfo: contactInfo{ + email: "asd@bsd.de", + zipCode: 1234, + }, + } + (&person1).updateName("asd") + person1.print() +} + +func (p *person) updateName(newFirstName string) { + (*p).firstName = newFirstName +} + +func (p person) print() { + fmt.Printf("%+v", p) +}