type Data struct {
value string
}
func (d Data) Value() string {
return d.value
}
type WithValue interface {
Value() string
}
d := Data{"hello"}
// Got a link error because of https://github.com/golang/go/issues/22998
d.Value()
hello
// This works without any problem.
WithValue(d).Value()
hello
import (
"fmt"
)
i := 0
go func() {
defer func() {
fmt.Println("Canceled")
}()
for i = 0;; i++ {}
}()
Canceled
import (
"fmt"
"time"
)
go func() {
panic("die!")
}()
time.Sleep(100 * time.Millisecond)
fmt.Println("main done")
panic: die! goroutine 26 [running]: runtime/debug.Stack(0xc4203f9eb8, 0x7fc12a067bc0, 0xc42003a010) /usr/lib/go-1.8/src/runtime/debug/stack.go:24 +0x7b github.com/yunabe/lgo/core.FinalizeGoRoutine() /home/yunabe/local/gocode/src/github.com/yunabe/lgo/core/core.go:81 +0x64 panic(0x7fc12a067bc0, 0xc42003a010) /usr/lib/go-1.8/src/runtime/panic.go:489 +0x2e7 github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init.func1.1() /home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:13 +0x66 github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init.func1() /home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:14 +0x40 created by github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init /home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:15 +0x3b
main done 10 <nil>
import (
"fmt"
)
type Hello interface {
SayHello()
}
type person struct {
name string
}
func (p *person) SayHello() {
fmt.Printf("Hello, I'm %s.\n", p.name)
}
p := person{"yunabe"}
fmt.Println("---- 1 ----")
p.SayHello()
var h Hello = &p
fmt.Println("---- 2 ----")
h.SayHello()
---- 1 ---- Hello, I'm yunabe. ---- 2 ---- Hello, I'm yunabe.
import (
"fmt"
"log"
"runtime"
"runtime/debug"
)
type MyData struct {
b []byte
}
func (m *MyData) Size() int {
return len(m.b)
}
func NewMyData() *MyData {
return &MyData{
b: make([]byte, 10 * (1 << 20)),
}
}
var l []*MyData
for i := 0; i < 100; i++ {
d := NewMyData()
l = append(l, d)
}
l = nil
debug.FreeOSMemory()
runtime.GC()
import (
"fmt"
)
for i := 0; i < 3; i++ {
go func(id int) {
fmt.Println("goroutine:", id)
}(i)
}
goroutine: 2 goroutine: 1 goroutine: 0