Skip to content

Instantly share code, notes, and snippets.

@MichaelDimmitt
Last active November 29, 2023 22:16
Show Gist options
  • Save MichaelDimmitt/08b7f916e46aef6fc30b5b590fcf0883 to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/08b7f916e46aef6fc30b5b590fcf0883 to your computer and use it in GitHub Desktop.
go by example guide

https://gobyexample.com
also https://go.dev/tour/welcome/1

all working except: https://gobyexample.com/timers

sections (future put an outlined based section.

In my opinion kind of dumb, https://stackoverflow.com/questions/6986944/does-the-go-language-have-function-method-overloading

why have a type system and no function overloading?

func plusPlus(a, b, c int) int {
	return a + b + c
}
func plusPlus(a, b, c string) int {
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(a)
	return 4
}
// fails

type any = interface{}
func plusPlus(a, b, c any) any {
	return a
}
// works

type Number interface {
  int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
type all = string | number Number | type | bool | error
func Sum[number Number](a number, b number) number {
  return a + b
}
// works but a + b would fail because it now is not of type number.

type All interface {
	// https://kuree.gitbooks.io/the-go-programming-language-report/content/3/text.html
	// string, numeric type, bool, and error
	rune | string | int | int8 | int16 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | bool
	// https://go.dev/ref/spec#Predeclared_identifiers
	// any bool byte comparable
	// complex64 complex128 error float32 float64
	// int int8 int16 int32 int64 rune string
	// uint uint8 uint16 uint32 uint64 uintptr
}
func plusPlus[number All](a, b, c number) number {
  return a + b + c	
}
// fails ... ./prog.go:40:9: invalid operation: operator + not defined on a (variable of type number constrained by All)


func alt(a, b int) int {
	return a + b
}

func plusPlus(a, b, c any) {
	switch t := a.(type) {
	case bool:
		return
	case int:
		fmt.Printf("is an int %T\n", t)
		var result = a.(int) + b.(int)
		fmt.Println("1+2+3 =", result)
		// return alt(a.(int), b.(int))
	default:
		fmt.Printf("Don't know type %T\n", t)
	}
	// return 3
	return
}
// works, uses type assertions https://go.dev/tour/methods/15
@MichaelDimmitt
Copy link
Author

MichaelDimmitt commented Oct 25, 2023

package main

import "fmt"

func plusPlus(a, b, c any) any {
	switch t := a.(type) {
	case bool:
		return a
	case int:
		fmt.Printf("is an int %T\n", t)
		var result = a.(int) + b.(int) + c.(int)
		return any(result)
	default:
		fmt.Printf("Don't know type %T\n", t)
	}
	return a
}

func main() {
	res2 := plusPlus(1, 2, 3)
	fmt.Println("works? =", res2)
}
// Today I wrote some bad go Code!
// This code type asserts to ints from any and then casts back to any when it returns.
// works: try it here: https://go.dev/play/

@MichaelDimmitt
Copy link
Author

links:
https://stackoverflow.com/questions/24337145/get-name-of-struct-field-using-reflection
https://stackoverflow.com/questions/41694647/how-do-i-use-reflect-to-check-if-the-type-of-a-struct-field-is-interface

Check the method list or interface of a struct.

package main

import "fmt"
import "reflect"

type A struct {
    Foo string
    Bar string
    Bas int
}

func (a *A) PrintFoo() {
    fmt.Println("Foo value is " + a.Foo)
}

func main() {
    a := &A{Foo: "afoo"}

    //long and bored code
    t := reflect.TypeOf(*a)
    if t.Kind() == reflect.Struct {
        for i := 0; i < t.NumField(); i++ {
            fmt.Println(t.Field(i).Name)
        }
    } else {
        fmt.Println("not a stuct")
    }

    //shorthanded call
    fmt.Println(reflect.TypeOf(*a).Field(0).Name)//can panic if no field exists

}

@MichaelDimmitt
Copy link
Author

package main

import "fmt"

type Number interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

func foo[number Number](a int, b int, c number) number {
	return a + b + c
}

func main() {
	res := foo(1, 2, 2)

	// var result = a.(int) + b.(int) + c.(int)
	fmt.Println("works? =", res)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment