Tucker's Golang programming Ch18 ~ Ch22
This post is a translation of the original Korean post.
This text is a summary of chapters 18 to 22 of the book “Golden Rabbit: [ Tucker’s Golang programming ]”.
Ch18 ~ Ch22
- Slice
- Method
- Interface
- Function
- Data Structure
# slice
Dynamic Arrays
- A data structure that automatically increases the size of an array
Declaration
|
|
# How slices work
Internal implementation
|
|
make() function
Allocates memory for heap and initializes it with zero value
- Used for slices, maps, and channels
Adding an element
Using the append() function
|
|
- Returns a new slice with the element added (same array pointer, len, cap)
internal behavior
- Check how much free space is left (cap - len)
- empty space >= number of elements added
- add to existing slice array as is
change only len
- Empty space < number of elements added
- create an array twice the size of the existing slice array
- copy all existing slice array elements to new array and add elements to it
- Replace array pointer with pointer to new array
Change array pointer, len, cap all
# Slicing
Pick up a portion of the array and slice and return it as a slice
|
|
- array pointer: points to a part of an existing array
- len: the extracted length
- cap: length from the start index to the end of the array (slice)
- When defining maxindex: Maxindex - startindex.
# Duplicate a slice
Duplicate into slices that point to different arrays with the same values.
Using the append() function
|
|
… keyword
Unpack slices into elements and pass them as arguments
- Receive as variable-length-argument on the receiving end (use as slices again inside the function)
Using the copy() function
|
|
# Add and delete slice elements
Delete an element
|
|
Add an element
|
|
# Sorting the slices
Using the default built-in package, the sort package
- Requires slices to implement Len(), Less(), Swap() methods (using alias types)
example
|
|
# Method
A type of function that belongs to a structure
Declaration
|
|
receiver
Tells you what type the method belongs to
- Struct, alias types
# Pointer methods vs value type methods
Value type receivers
All values of the receiver type are copied
- Another object when the object’s properties change
Pointer receiver
The address value of the memory the pointer is pointing to is copied
- Same object when object properties change
# Interface
A set of methods that does not contain an implementation
Declaration
|
|
# Duck typing
Implementing an interface
- Determined by including methods defined by the interface
- Not explicitly indicated
Service user-centered coding
- Interface is created and used by the user, not the service provider
💡 Create a relationship with a materialized object and use it later by creating an interface
# interface{}
Any type can be used because there are no methods to have
Type switch
switch t := v.(type)
- Compares types instead of values
- used for type-specific processing using an empty interface
# Type conversion
Use
|
|
❗Runtime error
Converting a type to a materialized type
- when converting to a different materialized type that implements the same interface
Converting a type to a different interface
- if the materialized type in the interface does not implement the interface being converted
Returns whether type conversion succeeded or failed
|
|
- No runtime errors
# Function
# Variable-argument function
Functions with an undetermined number of arguments
- Use the
...
keyword to indicate that it is a variable argument that takes multiple arguments of that type - Variable arguments are treated as slice types inside the function
|
|
# defer delayed execution
|
|
- Execute immediately before the function ends
- Execute multiple defer declarations in reverse order, starting with the last defer declared
# Function type variable
A variable whose value is a function
- Has a function start point (value pointing to a function)
Function type
|
|
Capture
Function Literal (Anonymous Function) ’s external variables into the local scope
Obtained by reference rather than value copying
- accessing the address of an external variable in pointer form
Dependency Injection
Function types and function literals are used to inject logic from outside
- This can also be achieved through interfaces
# Data Structures
# Lists
Store elements in non-contiguous memory by linking each data with pointers.
- Connecting the elements containing each piece of data with pointers
|
|
Array vs List
Operation | Array | List |
---|---|---|
Add to the Front | O(N) ∙ Shift each element one position ∙ Change the first value | O(1) ∙ Add an element to the front and link it |
Access a Specific Element | O(1) ∙ Start of the array + (Index x Type Size) | O(N) ∙ Traverse N-1 links |
- Array
- Favorable for frequent access.
- High data locality (how closely data is packed)
- List
- Ideal for frequent insertions and deletions.
Queue
Follow the FIFO structure for First-In, First-Out
Stack
Follow the FILO structure for First-In, Last-Out
Ring (Circular List)
A list that connects the beginning and the end
- Fixed in size
- Has no distinct start and end, just a current position
- Use
- Suitable for use when you have a fixed number of items, and old ones can be removed
# Map
A data structure that stores data in the form of key-value pairs
Creation
|
|
Iteration
|
|
Deletion of an Element
|
|
Checking for an Element
|
|
# Map Mechanism
Hash Function
- Same input -> Same output
- Inputs can be infinite, but outputs are within a specific range
- e.g.
- Trigonometric functions
- Modulus operation
Implementation Using Arrays
|
|
Handling Hash Collisions
- Store a list of key-value pairs instead of a single value