Tucker's Golang programming Ch12 ~ Ch17
This post is a translation of the original Korean post.
This text is a summary of chapters 12 to 17 of the book “Golden Rabbit: [ Tucker’s Golang programming ]”.
Ch12 ~ Ch17
- Array
- Structure
- Pointer
- String
- Packages
- Create a number matching game
# Array
A data structure with multiple values of the same type
|
|
When declaring an array, the number of elements is always a constant
range iteration
|
|
Continuous memory
- Element position
- Array start address + (index x type size)
copy array
Assignment operator:
Copies the value on the right-hand side into the memory space on the left-hand side
- Copy by the size of the type (must be of the same type)
# Structure
A type that bundles multiple fields of different types
|
|
Increased cohesion
- Group related data together
- Increases reusability
Initializes structs
- Omit initialization
- All fields are initialized to default values (zero value)
- Initialize some fields
- Rest of the fields are initialized with default values
Copy Structure Values
All field values in the structure are copied
# Structures containing structs
Can contain other structures as fields, just like regular types
field name omitted (included fields)
- Child structures (struct fields) are directly accessible through parent structure variables
- Overlapping field names in parent and child structures
- Use fields from the parent structure
- fields in the child structure are accessed using the child structure name
# Memory padding
register size
Size that can be used for a single operation
- 32-bit computers: 4 bytes
- 64-bit computers: 8 bytes
Spacing between fields for memory alignment
- Align the number of bytes and starting address of the variable to a multiple (e.g. 4-byte variable: starting address that is a multiple of 4)
- Variables of type 8 bytes on 64-bit computers
- Since the register size is 8 bytes, it is a performance penalty if it is not allocated at an address that is a multiple of 8
# pointer
Types whose value is a memory address
- Memory Addresses
- Can be imported by prefixing regular variables with
&
- Can be imported by prefixing regular variables with
# Pointer variables
Variables that can have a memory address as their variable value
- 8 bytes in size on a 64-bit computer
Declarations
|
|
Accessing
Access pointer variables by prefixing them with *
|
|
nil
Indicates that no memory space is being pointed to
new() built-in function
allocates a type into memory, fills it with default values, and returns its address
# Escape analysis
Allocate memory by checking whether the memory space is public outside the function
Instances that are visible outside of the function (returning a memory address)
- Does not disappear when function terminates
- Allocate to heap memory, not stack.
# String
Set of characters
Expressions
- Double quotes (
"
): special character behavior - Backquote ( ` ): treats special characters as regular characters
- Can write strings across multiple lines
rune type
- Represents a single character
- Alias type of int32 type (4 bytes)
- Can output a single character using the
%c
format
len()
- a string
- Returns the amount of memory (in bytes) occupied by the string
- slice
- Returns the number of elements in an array
# Type conversions
- []rune
- an array of individual characters
- []byte
- Array of 1 bytes
# String iteration
- Byte-by-byte iteration: use indexes
1 2 3
for i := 0; i < len(str); i++ { str[i] }
- Single-letter iteration
- Using the []rune type
1 2 3 4
arr := []rune(str) for i := 0; i < len(arr); i++ { arr[i] }
- Using the range keyword
1 2 3
for _, v := range str { v // rune type }
- Using the []rune type
# string structure
- Data: uintptr (8 bytes)
- A pointer to the memory address of the string
- Len: int (8 bytes)
- Length of the string
# Immutability
Difficult to track without immutability because we’re pointing to the string via a pointer
- Global change is possible
- Data and Len values change
- Cannot be partially changed through indexes
- Copying a string to a new memory space
- When casting to a slice type
- When summing strings
- Recommend using
strings.builder
for frequent use.
- Recommend using
# package
Largest unit to bundle code into
main package
- Package containing the main() function (program start point)
# Installing packages
- Packages provided by the go language
- Found in the go installation path
- Packages stored in external storage
- Downloaded from external storage to match the version defined in go modules
- Packages located under the current module
- Look for packages under the current folder
# Initializing packages
- import the package
- initialize global variables in the package
- call the init() function in the package
- init() function conditions: a function with no input parameters and no return value.
# Create a number matching game
Guess a randomly selected number from the program
- run the program
- enter the expected number in the console
- Exit the program if the answer is correct
- If it’s less than or equal to, print a message
- View the message and repeat from step 2
|
|