Nesting Structs
07/26/21 08:30 Filed in: GO
Last time I learned about how to use pointers in GO to pass strutcts to functions. Today, I figure out if there is anything I should know about nesting a struct within another struct. Read More…
Structs and Pointers
07/12/21 08:35 Filed in: GO
Last time I mentioned that GO passes parameters by value, so modifying a struct within a function takes extra work. If my structs are large, there may be memory pressure as a result of creating copies in functions. That's where passing a pointer to a struct can help. That's what I tackle this time. Read More…
Structs in GO
06/28/21 08:27 Filed in: GO
I've learned about maps and slices in GO. However, they can only hold items of uniform types. Today, I dive into learning about structs which can hold a combination of different types.
Structs are a common data structure in many programming languages and are useful for managing real-world records and related information of different types. The syntax is also similar to that of other languages.
var name struct {
variable type
variable type
variable type
…
}
You can have any number of variables in a struct. Like other things in GO, the default values are the appropriate zero values for the data types. Here is an example:

In order to set values you use the dot operator.

This is similar to how other programming languages do it, as well.
If I need more than one record based on aStruct, I can either repeat the definition of the struct, or I can create a new type based on it. I can then declare variables based on the type. So, declare once, use often. Here is the syntax for a defined type based on a struct.

This looks like the same code. There is one major difference: the type keyword replaces the var keyword. Once defined this way, I can declare variables based on the type:

Being a type, I can pass a struct into a function as an argument.

One thing I need to remember is that GO passes parameters by value, so modifying a struct within a function takes extra work. It's better to treat GO functions as pure functions and return a modified copy.
There is an issue. If my structs are large, there may be memory pressure as a result of creating copies in functions. That's where passing a pointer to to a struct can help.
I'll tackle that next time.
Structs
Structs are a common data structure in many programming languages and are useful for managing real-world records and related information of different types. The syntax is also similar to that of other languages.
var name struct {
variable type
variable type
variable type
…
}
You can have any number of variables in a struct. Like other things in GO, the default values are the appropriate zero values for the data types. Here is an example:

Assigning Values to a Struct
In order to set values you use the dot operator.

This is similar to how other programming languages do it, as well.
Type Definition
If I need more than one record based on aStruct, I can either repeat the definition of the struct, or I can create a new type based on it. I can then declare variables based on the type. So, declare once, use often. Here is the syntax for a defined type based on a struct.

This looks like the same code. There is one major difference: the type keyword replaces the var keyword. Once defined this way, I can declare variables based on the type:

Structs and Functions
Being a type, I can pass a struct into a function as an argument.

One thing I need to remember is that GO passes parameters by value, so modifying a struct within a function takes extra work. It's better to treat GO functions as pure functions and return a modified copy.
There is an issue. If my structs are large, there may be memory pressure as a result of creating copies in functions. That's where passing a pointer to to a struct can help.
I'll tackle that next time.
Iterating Maps
06/14/21 08:42 Filed in: GO
Last time I learned about maps in GO and how to create, change, and delete them. Today, I go over iterating over a map. Read More…