-
gci write --skip-generated -s standard -s default -s localmodule .
-
What does the . in a Go import statement do
- Allows functions inside the package to be called directly, but is not recommended
-
When should you use
errors.Asand when to useerrors.Isfor your own custom errorserrors.Isforvarerrors,errors.Asfor struct errors that has to be initialized (especially if it has custom fields)
-
How to test a unexported (private) function in go (golang)?, the article
-
When and why to use functional options pattern. 1 2:
- Default values should be useful, using a struct the default values is implicitly communicated in the struct, and its better to give useful default values. Using functional options it is implicitly done in the
NewXXXfunctions. (In other words when it is hard to distinguish default values from a valid setting) - Functional options can return errors
- Functional options allow setting a combination of values like
WithAws,WithAzureetc. Users can also create their own useful combinations.
- Default values should be useful, using a struct the default values is implicitly communicated in the struct, and its better to give useful default values. Using functional options it is implicitly done in the
-
Why append a nil slice is OK, while assign a nil map isn't OK?
-
Why recover() can only be used in a defer function and not directly
- a.k.a.
defer recover()doesnt work butdefer func() {recover()}does - extra
- a.k.a.
-
Breaking out of a select statement when all channels are closed
-
Mastering Go Structs: 7 Advanced Techniques for Efficient Code
-
Go interface guards, for when there are no static conversions in code
-
Function types and single-method interfaces
- You can use function types to mock stuff, wow
-
Limiting amount of parallel goroutines with buffered channels
-
Type assertion vs type switches
- Apparently you can use a variable in a type switch case
-
Issues with comparing pointers of zero sized type
- Language spec states that equality of pointers to distinct zero size variables is unspecified
-
- Don't close a channel from the receiver side
- Don't close a channel if the channel has multiple concurrent senders
-
There is no memory safety without thread safety
- data_race_issue.go
- Go stores values of interface types like Thing as pairs of a pointer to the data and a pointer to the vtable. Every time repeat_swap stores a new value in globalVar, it just does two separate stores to update those two pointers. In repeat_get, there’s thus a small chance that when we read globalVar in between those two stores, we get a mix of a pointer to an Int with the vtable for a Ptr. When that happens, we will run the Ptr version of get, which will dereference the Int’s val field as a pointer – and hence the program accesses address 42, and crashes.
- Can be solved by mutex
-
7 Deadly Mistakes Beginner Go Developers Make (and how to fix them)
- You can label loops apparently
- map, slice, values in an interface are not addressable
- Pointer receivers can only accept pointers, while value receivers can accept both values and pointers
-
Golang struct configuration pattern
- Use a struct to pass optional values
- Allow the constructing function to take a list of functions that can modify the options to allow for extensibility
-
GopherCon 2016: Francesc Campoy - Understanding nil
- 16:30
- Use nil to disable channels
- Nil is valid as a read only empty map
-
The standard library now has all you need for advanced routing in Go
-
GothamGo 2018 - Things in Go I Never Use by Mat Ryer
- Lazy initialization (20:54)
sync.Once: Guarantees thing inside code block will be called exactly once
- You can pass {the struct you want to work on} to {a method called on the struct using dot, ex.
Struct.method(actualObject)}, don't do this but its interesting (22:20)
- Lazy initialization (20:54)
-
Golang UK Conference 2016 - Dave Cheney - SOLID Go Design
- Single Responsibility Principle
- Structure functions and types into packages that exhibit natural cohesion
- Avoid
shared,utils, which causes the packages to have multiple responsibilities
- Open / Closed Principle
- Open for extension, closed for modification
- Use embedding rather than inheritanca
- Liskov Substitution Principle
- Implicit interfaces, use small interfaces and express the dependencies between packages as interfaces
- Require no more, promise no less
- Interface Segregation Principle
- Clients should not be forced to depend on methods they don't use
- Ex. A
Savefunction should only need aWriter, instead ofReadorFile, or evenClose, therefore it can work with network writer, file writers etc
- Dependency Inversion Principle
- High level modules should not depend on low level modules, both should depend on abstractions
- Abstractions should not depend on details. Details should depend on abstractions
- Import graph should be acyclic, push responsibility as high as possible in the import tree
- Single Responsibility Principle
-
Golang UK Conference 2016 - Mat Ryer - Idiomatic Go Tricks
- 9:16:
defer log.Println("------")even if exits abnormally this code will print - defer can also be used for teardown functions for any setup / timers that need to stop at end of the line
- 21:18: Retry code
- 22:10: Empty structs to group methods methods don't capture the receiver, so the variable need not expose the private type
- 23:40: Semaphore code, limit hom many stuff is executed at once
- 9:16:
-
How To Build A Chat And Data Feed With WebSockets In Golang?
runtime.NumCPUruntime.GOMAXPROCS()
-
Docker distroless Docker exec failed: No such file or directory when CGO is enabled
-
- this is really really really fuckin good
-
- It allows to show different fields depending on what the runtime type for the interface is
-
- Use objects for input