Skip to content

Instantly share code, notes, and snippets.

@gorshkov-leonid
Last active August 19, 2025 11:26
Show Gist options
  • Save gorshkov-leonid/e2a6d4569836a14c4b8e0d0bff46cd29 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/e2a6d4569836a14c4b8e0d0bff46cd29 to your computer and use it in GitHub Desktop.
Go Examples

// reference to receiver

package main
import "fmt"

type Obj struct {
	f  string
}

func (o *Obj) Log() {
	fmt.Println(o.f)
}

func main(){
	o := Obj{}
	o.f = "1"
	o.Log()
	call(o.Log)
}

func call(f func() ) {
    f();
}

// rewrite allocated memory

package main

func main() {
   	aaaa := "1"
	bbbb := "2"
	a := &aaaa
	
	println(*a)
	
	*a = bbbb
	println(*a)
}

// invalid cast

package main

type Obj1 struct {f1  string; f2  string }
type Obj2 struct { f1  string }

func main(){
	o1 := Obj1{
	   f1: "1",
	   f2: "2",
	}
    o2:= Obj2(o1)  // cannot convert o1 (type Obj1) to type Obj2
    // o2:= o1.(Obj2) // invalid type assertion: o1.(Obj2) (non-interface type Obj1 on left)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment