Last active
November 3, 2025 19:36
-
-
Save holmberd/edf1e94fd9788b4171fdc26d3c60cbd9 to your computer and use it in GitHub Desktop.
Revisions
-
holmberd revised this gist
Nov 3, 2025 . 1 changed file with 32 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -207,4 +207,35 @@ func (*S) G() {} var _ I = (*S)(nil) // *S Satisifes the interface I. var _ I = S{} // S does not satisfy the interface I. ``` # Monomorphization (Compiler Escape Analysis inlining) ```go import "github.com/cespare/xxhash/v2" type Hasher struct{} // Hash implements the Hasher interface. // Because this is a direct call on a concrete type, // the compiler can inline it. func (h XxHasher) Hash(key []byte) uint64 { return xxhash.Sum64(key) } func (b *Buffer) Compact[H Hasher]() (done bool, newOffsets [][2]uint64, err error) { // Get a zero-value instance of the Hasher type. // Since our XxHasher is an empty struct, // this variable is zero-cost and compiles away. var hasher H // This is now a direct call on a concrete type, // not an indirect function-pointer call. hash := hasher.Hash(b.keyBuf) // ... } b := Buffer{} b.Compact[Hasher]() // Example usage. ``` -
holmberd revised this gist
Mar 7, 2025 . 1 changed file with 26 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,9 @@ ```go // https://www.dolthub.com/blog/2023-10-20-golang-pitfalls-3/ type slice struct { array unsafe.Pointer len int cap int } sliceA := []int{1, 2, 3} @@ -28,16 +28,16 @@ func (m *MyType) Write(p []byte) (n int, err error) { } type MyInterface interface { MyMethod() } func main() { var myType *MyType // myType = nil test(myType) var myInterface MyInterface if myInterface == nil { fmt.Println("The interface is nil.") } } @@ -68,7 +68,7 @@ func main() { ## Interface array/slice E.g. passing a slice of a type(A) as the argument to a function. Type A satisifes the function, but `[]A` does not. Convert the type array to an interface array. ```go type RequirementConstrained interface { GetRequirements() []Requirement } @@ -107,17 +107,17 @@ func main() { # Strings ```go func main() { str := "你好" // Chinese characters for "Hello" // Accessing individual bytes (byte = uint8) in the string for i := 0; i < len(str); i++ { fmt.Printf("%U)", str[i]) } fmt.Println() // Accessing individual runes (Unicode code points - UTF8, rune = int32) in the string for _, r := range str { fmt.Printf("%c ", r) } fmt.Println() } @@ -192,4 +192,19 @@ func main() { fmt.Println("Type assertion failed") } } ``` # Interface Pointer Receivers ```go type I interface { F() G() } type S struct{} func (S) F() {} func (*S) G() {} var _ I = (*S)(nil) // *S Satisifes the interface I. var _ I = S{} // S does not satisfy the interface I. ``` -
holmberd revised this gist
Dec 12, 2024 . 1 changed file with 40 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,8 @@ fmt.Println(*(*slice)(unsafe.Pointer(&sliceA))) // prints {0xc00001a018 3 3} ``` # Interfaces ## Not nil interface If you assign a value to an interface, even if that value is nil, the interface itself is not nil. ```go type MyType struct{} @@ -64,6 +66,44 @@ func main() { } ``` ## Interface array/slice E.g. passing a slice of a type(A) as the argument to a function. Type A satisifes the function, but `[]A` does not. Convert the type array to an interface array. ``` type RequirementConstrained interface { GetRequirements() []Requirement } type Requirement int type A struct { requirements []Requirement } func (a A) GetRequirements() []Requirement { return a.requirements } type B struct { requirements []Requirement } func (b B) GetRequirements() []Requirement { return b.requirements } func FilterByRequirements(entity RequirementConstrained) RequirementConstrained { return entity } func main() { var aents []A var filtered []RequirementConstrained for _, a := range aents { filtered = append(filtered, FilterByRequirements(a)) } } ``` # Strings ```go func main() { -
holmberd revised this gist
Dec 9, 2023 . 1 changed file with 54 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -98,4 +98,58 @@ a = &someString{"test1"} b = &someString{"test1"} fmt.Println(a == b) // false ``` # Reflect (Typeof) `reflect.TypeOf(v).String()` # Type Assertion (InstanceOf) ```go type MyInt interface{} var x MyInt = 5 res := x.(int) fmt.Printf("type: %T value: %[1]v\n", res) ``` # Interface assertion ```go // Animal interface type Animal interface { Speak() string } // Trainer interface with an additional method type Trainer interface { Train() string } // Dog type implementing both Animal and Trainer interfaces type Dog struct{} func (d Dog) Speak() string { return "Woof! Woof!" } func (d Dog) Train() string { return "Sit! Stay!" } type SpeakTrain interface { Speak() string Train() string } func main() { var t Trainer t = PoliceDog{} // Type assertion to access the larger set of methods in the Trainer interface if st, ok := x.(SpeakTrain); ok { fmt.Println("Train:", st.Train()) fmt.Println("Speak:", st.Speak()) } else { fmt.Println("Type assertion failed") } } ``` -
holmberd revised this gist
Dec 6, 2023 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -81,4 +81,21 @@ func main() { } fmt.Println() } ``` # Equality When comparing two instances of a value type in Go, it checks whether their field values are equal. ```go type someStr struct{ text string } a := someString{"test1"} b := someString{"test1"} fmt.Println(a == b) // true a = &someString{"test1"} b = &someString{"test1"} fmt.Println(a == b) // false ``` -
holmberd revised this gist
Dec 3, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -71,7 +71,7 @@ func main() { // Accessing individual bytes (byte = uint8) in the string for i := 0; i < len(str); i++ { fmt.Printf("%U)", str[i]) } fmt.Println() -
holmberd revised this gist
Dec 3, 2023 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,4 +62,23 @@ func main() { fmt.Println("The interface is nil.") } } ``` # Strings ```go func main() { str := "你好" // Chinese characters for "Hello" // Accessing individual bytes (byte = uint8) in the string for i := 0; i < len(str); i++ { fmt.Printf("%c ", str[i]) } fmt.Println() // Accessing individual runes (Unicode code points - UTF8, rune = int32) in the string for _, r := range str { fmt.Printf("%c ", r) } fmt.Println() } ``` -
holmberd revised this gist
Dec 3, 2023 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,20 @@ # Slices ```go // https://www.dolthub.com/blog/2023-10-20-golang-pitfalls-3/ type slice struct { array unsafe.Pointer len int cap int } sliceA := []int{1, 2, 3} sliceB := append(sliceA, 4) sliceC := append(sliceA, 5) sliceC[0] = 0 fmt.Println(*(*slice)(unsafe.Pointer(&sliceA))) // prints {0xc00001a018 3 3} ``` # Interfaces If you assign a value to an interface, even if that value is nil, the interface itself is not nil. ```go -
holmberd created this gist
Dec 2, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ # Interfaces If you assign a value to an interface, even if that value is nil, the interface itself is not nil. ```go type MyType struct{} func (m *MyType) Write(p []byte) (n int, err error) { fmt.Println("called.") return len(p), nil } type MyInterface interface { MyMethod() } func main() { var myType *MyType // myType = nil test(myType) var myInterface MyInterface if myInterface == nil { fmt.Println("The interface is nil.") } } func test(out io.Writer) { if out != nil { fmt.Println("interface is not nil.") } if v, ok := out.(*MyType); ok && v == nil { fmt.Println("Underlying value of the interface is nil.") } } ``` ```go type MyInterface interface { MyMethod() } func main() { var myInterface MyInterface if myInterface == nil { fmt.Println("The interface is nil.") } } ```