Structs

Reading material

A Tour of Go: Structs. Go by Example: Structs, Struc Embedding.


First exercise


// structs1
// Make me compile!
//
// I AM NOT DONE
package main

import "fmt"

type Person struct {

}

func main() {
	fmt.Printf("Person %s and age %d", person.name, person.age)
}

Second exercise

// structs2
// Make me compile!
//
// I AM NOT DONE
package main

import "fmt"

type Person struct {
	// don't just create the phone field here. embed a new struct
	name string
	age int
}

func main() {
	// contactDetails := ContactDetails{}
	person := Person{name: "John", age: 32}
	fmt.Printf("%s is %d years old and his phone is %s\n", person.name, person.age, person.phone)
}

Third exercise


// structs3
// Make me compile!
//
// I AM NOT DONE
package main

import "fmt"

type Person struct {
	firstName string
	lastName  string
}

func main() {
	person := Person{firstName: "Maurício", lastName: "Antunes"}
	fmt.Printf("Person full name is: %s\n", person.FullName()) // here it must output Person full name is: Maurício Antunes
}


If something is wrong in this page, edit this page on GitHub