Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:02
Show Gist options
  • Save dacr/c00e48cda293194986797de8f01fca01 to your computer and use it in GitHub Desktop.
Save dacr/c00e48cda293194986797de8f01fca01 to your computer and use it in GitHub Desktop.
go generics / published by https://github.com/dacr/code-examples-manager #34fb816b-0528-47fe-91b5-0173ffa670a5/329135f1b6b086e920b59ac89cd1998a234afcf4
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/
// summary : go generics
// keywords : go, generics, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 34fb816b-0528-47fe-91b5-0173ffa670a5
// created-on : 2025-03-27T09:36:13+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : nix-shell -p go --run "go run $file"
package main
import (
"fmt"
"math/rand"
"slices"
)
func dataTypes() {
var flag bool = rand.Intn(2) == 1
var myb byte = 42
var myi int = 42
var myi8 int8 = 42
var myi16 int16 = 42
var myi32 int32 = 42
var myi64 int64 = 42
var myui uint = 42
var myui64 uint64 = 42
var myf32 float32 = 42.0
var myf64 float64 = 42.0
var mystr string = "Hello World"
var myrune rune = 'a'
var myIntArray [5]int = [5]int{1, 2, 3, 4, 5} // fixed size array
var myOtherArray [5]int = [...]int{1, 2, 3, 4, 5} // fixed size array
var myIntSlice = []int{} // dynamic array
var myOtherIntSlice = []int{1, 2, 3}
myIntSlice = append(myIntSlice, 1, 2, 3)
myOtherIntSlice = append(myIntSlice, 4, 5, 6) // immutable operation
zeroValuesSlice := make([]int, 5)
for i, char := range mystr {
var chrune rune = char
fmt.Printf("Index: %d, Character: %c - %c\n", i, char, chrune)
}
fmt.Println(flag, myb, myi, myi8, myi16, myi32, myi64, myui, myui64, myf32, myf64, mystr, myrune)
fmt.Println(myIntArray, myOtherArray)
fmt.Println(myIntSlice, myOtherIntSlice)
fmt.Println(zeroValuesSlice)
fmt.Println(slices.Concat(myIntSlice, myOtherIntSlice))
array := [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} //
fmt.Println(array[0:10])
fmt.Println(array[5:])
fmt.Println(array[:5])
fmt.Println(array[:])
fmt.Println(len(array[7:]))
from := 7
fmt.Println(len(array[from:]))
slice := array[4:6]
display("array1", array, slice)
slice[1] = 666
display("array2", array, slice)
// ATTENTION le slice pointe sur le tableau sous-jacent
// ET un append sur le slice va venir modifier écraser une valeur dans le tab sous-jacent
// => Attention aux effets de bords
// => Evite les recopies !
// attention si le slice pointe vers la fin du tableau => risque de débordement en fin du tableau !!
// quand on étend un slice alors que le tableau sous-jacent est trop petit alors
// recopie du tableau ! et donc nouvelle capacité
// ATTENTION DONC AUSSI AUX SLICES DE SLICES !
}
func display(msg string, array [10]int, slice []int) {
fmt.Println("-------------------------------")
fmt.Println(msg)
fmt.Println("array", array, "len=", len(array), " - ", "cap=", cap(array))
fmt.Println("slice", slice, "len=", len(slice), " - ", "cap=", cap(slice))
fmt.Println(slice)
}
func makeSlicesNoCopy() {
slice := make([]int, 20) // pour éviter les recopies !
for i := range 10 {
slice = append(slice, i) // pas de recopie, mutation
}
fmt.Println(slice)
}
func makeSlicesCopy() {
slice := make([]int, 0, 1) // pour éviter les recopies !
for i := range 10 {
slice = append(slice, i) // recopie réallocation
}
fmt.Println(slice)
}
func mapDataType() {
var myMap = map[string]int{
"key1": 1,
"key2": 2,
"key3": 3,
}
fmt.Printf("key1=%d\n", myMap["key1"])
myMap["key42"] = 42
fmt.Printf("key42=%d\n", myMap["key42"])
delete(myMap, "key42")
_, ok := myMap["key42"]
if !ok {
fmt.Println("key42 not found")
}
var nilMap map[string]string // not allocated so adding key is impossible
//nilMap["xx"] = "panic: assignment to entry in nil map"
println(nilMap == nil)
var emptyMap = make(map[string]string)
println(emptyMap == nil)
}
func main() {
dataTypes()
makeSlicesNoCopy()
makeSlicesCopy()
mapDataType()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment