-
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.As
and when to useerrors.Is
for your own custom errorserrors.Is
forvar
errors,errors.As
for 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
NewXXX
functions. (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
,WithAzure
etc. 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
-
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
-
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
Save
function should only need aWriter
, instead ofRead
orFile
, 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.NumCPU
runtime.GOMAXPROCS()
-
- It allows to show different fields depending on what the runtime type for the interface is
-
- Use objects for input