Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 2.84 KB

19-structs.md

File metadata and controls

93 lines (71 loc) · 2.84 KB

This Go program demonstrates the usage of structs, creating instances of a struct, initializing struct fields, and working with pointers to structs.

Let's go through each part of the code with inline comments and additional explanations:

package main

import "fmt"

// Define a struct named 'person' with two fields: 'name' of type string and 'age' of type int
type person struct {
    name string
    age  int
}

// Function newPerson creates and initializes a new person, setting the name and age, and returns a pointer to the person struct
func newPerson(name string) *person {
    p := person{name: name}
    p.age = 42
    return &p
}

func main() {
    // Create an instance of the person struct with field values "Bob" and 20, and print it
    fmt.Println(person{"Bob", 20})

    // Create an instance of the person struct with named field values, "Alice" and 30, and print it
    fmt.Println(person{name: "Alice", age: 30})

    // Create an instance of the person struct with a named field value, "Fred," and print it
    fmt.Println(person{name: "Fred"})

    // Create an instance of the person struct using the newPerson function, setting the name to "Ann" and age to 40, and print it
    fmt.Println(newPerson("Ann"))

    // Create an instance of the person struct using the newPerson function, setting the name to "Jon", and print it
    fmt.Println(newPerson("Jon"))

    // Create an instance of the person struct with field values "Sean" and 50
    s := person{name: "Sean", age: 50}

    // Access and print the 'name' field of the person struct
    fmt.Println(s.name)

    // Create a pointer to the person struct
    sp := &s

    // Access and print the 'age' field of the person struct using a pointer
    fmt.Println(sp.age)

    // Modify the 'age' field of the person struct using the pointer
    sp.age = 51

    // Print the modified 'age' field of the person struct
    fmt.Println(sp.age)

    // Create an instance of an anonymous struct representing a dog and print it
    dog := struct {
        name   string
        isGood bool
    }{
        "Rex",
        true,
    }
    fmt.Println(dog)
}

Output

{Bob 20}
{Alice 30}
{Fred 0}
&{Ann 40}
&{Jon 42}
Sean
50
51
{Rex true}

Explanation:

  1. A person struct is defined with two fields: name of type string and age of type int.

  2. The newPerson function creates and initializes a new person, setting the name and age, and returns a pointer to the person struct.

  3. In the main function:

    • Instances of the person struct are created with different field values and printed.
    • The newPerson function is used to create instances with default values and print them.
    • Field values of an instance are accessed and printed.
    • A pointer to a person struct is created and used to access and modify field values.
    • An anonymous struct representing a dog is created and printed.