Structs in GO Programming Language

go-logo

Structs in GO programming language are composite data types that allow you to group together zero or more values with different data types. They are similar to classes in object-oriented programming languages, but with some differences.

In GO, a struct is defined using the type keyword followed by the name of the struct and the keyword struct. The fields of the struct are defined within curly braces {}. The fields can have different data types, including other structs, arrays, and slices.

Creating a Struct

To create a struct, you first need to define the struct type. Here’s an example of a struct type definition:

type Person struct {
    Name string
    Age int
    Address string
}

This defines a struct type called Person with three fields: Name, Age, and Address. To create a new Person struct, you can use the Person type and initialize its fields:

person := Person{
    Name: "John",
    Age: 30,
    Address: "123 Main St",
}

You can also create a struct without initializing its fields, in which case the fields will be set to their default values. For example:

var person Person

This creates a new Person struct with the Name, Age, and Address fields set to their default values (empty string for Name and Address, and 0 for Age).

Initializing a Struct

You can also initialize a struct using the new keyword, which creates a new instance of the struct and returns a pointer to it:

personPtr := new(Person)
personPtr.Name = "Jane"
personPtr.Age = 25
personPtr.Address = "456 Elm St"

This creates a new Person struct and initializes its fields by accessing them through the pointer.

Another way to initialize the struct fields is to use the “struct literal” syntax, which allows you to specify only some of the fields:

person := Person{
    Name: "John",
    Age: 30,
}

This creates a new Person struct with the Name and Age fields set, but with the Address field set to its default value.

Accessing Struct Fields

You can access the fields of a struct using the dot . operator:

fmt.Println(person.Name)
fmt.Println(person.Age)
fmt.Println(person.Address)

This will print out the values of the fields in the person struct.

Nested Structs

GO programming language allows you to define nested structs, which are structs that have other structs as fields. Here’s an example:

type Address struct {
    Street string
    City string
    State string
}

type Person struct {
    Name string
    Age int
    Address Address
}

This defines a struct type called Person with three fields: Name, Age, and Address. The Address field is itself a struct with three fields: Street, City, and State. To create a new Person struct with an Address field, you can use the following syntax:

person := Person{
    Name: "John",
    Age: 30,
    Address: Address{
        Street: "123 Main St",
        City: "New York",
        State: "NY",
    },
}

This creates a new Person struct with an Address field that contains a new Address struct with the specified values.

Structs are a powerful feature of GO programming language that allow you to group related data together in a structured way. With this knowledge, you should be able to create and work with structs in your own GO programs.

Total
0
Shares
Previous Post
go-logo

Pointers: Understanding Pointers in GO Programming Language

Next Post
go-logo

Interfaces: Understanding interfaces in the GO programming language, how to define, implement, and use them in your code.

Related Posts