2018-05-04 17:31:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-12 23:24:31 +00:00
|
|
|
"os"
|
2018-05-04 17:31:45 +00:00
|
|
|
"runtime"
|
2018-07-28 19:12:07 +00:00
|
|
|
"strings"
|
2018-05-04 17:31:45 +00:00
|
|
|
)
|
|
|
|
|
2020-11-12 23:24:31 +00:00
|
|
|
var call = "this is a variable named `call`"
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
func callstacktrace() (stacktrace string) {
|
|
|
|
for skip := 0; ; skip++ {
|
|
|
|
pc, file, line, ok := runtime.Caller(skip)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fn := runtime.FuncForPC(pc)
|
|
|
|
stacktrace += fmt.Sprintf("in %s at %s:%d\n", fn.Name(), file, line)
|
|
|
|
}
|
|
|
|
return stacktrace
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:24:31 +00:00
|
|
|
func call0(a, b int) {
|
|
|
|
fmt.Printf("call0: first: %d second: %d\n", a, b)
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
func call1(a, b int) int {
|
|
|
|
fmt.Printf("first: %d second: %d\n", a, b)
|
|
|
|
return a + b
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:24:31 +00:00
|
|
|
func call2(a, b int) (int, int) {
|
|
|
|
fmt.Printf("call2: first: %d second: %d\n", a, b)
|
|
|
|
return a, b
|
|
|
|
}
|
|
|
|
|
|
|
|
func callexit() {
|
|
|
|
fmt.Printf("about to exit\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
func callpanic() {
|
|
|
|
fmt.Printf("about to panic\n")
|
|
|
|
panic("callpanic panicked")
|
|
|
|
}
|
|
|
|
|
2020-11-12 23:24:31 +00:00
|
|
|
func callbreak() {
|
|
|
|
fmt.Printf("about to break")
|
|
|
|
runtime.Breakpoint()
|
|
|
|
}
|
|
|
|
|
2018-07-28 19:12:07 +00:00
|
|
|
func stringsJoin(v []string, sep string) string {
|
|
|
|
// This is needed because strings.Join is in an optimized package and
|
|
|
|
// because of a bug in the compiler arguments of optimized functions don't
|
|
|
|
// have a location.
|
|
|
|
return strings.Join(v, sep)
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:50:10 +00:00
|
|
|
type astruct struct {
|
|
|
|
X int
|
|
|
|
}
|
|
|
|
|
2018-08-18 08:53:21 +00:00
|
|
|
type a2struct struct {
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:50:10 +00:00
|
|
|
func (a astruct) VRcvr(x int) string {
|
|
|
|
return fmt.Sprintf("%d + %d = %d", x, a.X, x+a.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *astruct) PRcvr(x int) string {
|
|
|
|
return fmt.Sprintf("%d - %d = %d", x, pa.X, x-pa.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PRcvrable interface {
|
|
|
|
PRcvr(int) string
|
|
|
|
}
|
|
|
|
|
|
|
|
type VRcvrable interface {
|
|
|
|
VRcvr(int) string
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
var zero = 0
|
|
|
|
|
2018-07-31 16:32:30 +00:00
|
|
|
func makeclos(pa *astruct) func(int) string {
|
|
|
|
i := 0
|
|
|
|
return func(x int) string {
|
|
|
|
i++
|
|
|
|
return fmt.Sprintf("%d + %d + %d = %d", i, pa.X, x, i+pa.X+x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:17:22 +00:00
|
|
|
var ga astruct
|
2018-08-18 08:53:21 +00:00
|
|
|
var globalPA2 *a2struct
|
|
|
|
|
|
|
|
func escapeArg(pa2 *a2struct) {
|
|
|
|
globalPA2 = pa2
|
|
|
|
}
|
2018-08-17 06:17:22 +00:00
|
|
|
|
2019-05-09 15:29:58 +00:00
|
|
|
func square(x int) int {
|
|
|
|
return x * x
|
|
|
|
}
|
|
|
|
|
|
|
|
func intcallpanic(a int) int {
|
|
|
|
if a == 0 {
|
|
|
|
panic("panic requested")
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func onetwothree(n int) []int {
|
|
|
|
return []int{n + 1, n + 2, n + 3}
|
|
|
|
}
|
|
|
|
|
2019-05-30 15:08:37 +00:00
|
|
|
func curriedAdd(n int) func(int) int {
|
|
|
|
return func(m int) int {
|
|
|
|
return n + m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAStruct(n int) astruct {
|
|
|
|
return astruct{X: n}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAStructPtr(n int) *astruct {
|
|
|
|
return &astruct{X: n}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVRcvrableFromAStruct(n int) VRcvrable {
|
|
|
|
return astruct{X: n}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPRcvrableFromAStructPtr(n int) PRcvrable {
|
|
|
|
return &astruct{X: n}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVRcvrableFromAStructPtr(n int) VRcvrable {
|
|
|
|
return &astruct{X: n}
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:51:29 +00:00
|
|
|
func noreturncall(n int) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-18 22:19:06 +00:00
|
|
|
type Base struct {
|
2019-09-26 14:37:23 +00:00
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Derived struct {
|
|
|
|
x int
|
|
|
|
Base
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Base) Method() int {
|
|
|
|
return b.y
|
|
|
|
}
|
|
|
|
|
2020-02-18 22:19:06 +00:00
|
|
|
type X int
|
|
|
|
|
|
|
|
func (_ X) CallMe() {
|
|
|
|
println("foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
type X2 int
|
|
|
|
|
|
|
|
func (_ X2) CallMe(i int) int {
|
|
|
|
return i * i
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
func main() {
|
|
|
|
one, two := 1, 2
|
2018-07-28 19:12:07 +00:00
|
|
|
intslice := []int{1, 2, 3}
|
|
|
|
stringslice := []string{"one", "two", "three"}
|
|
|
|
comma := ","
|
2018-07-31 12:50:10 +00:00
|
|
|
a := astruct{X: 3}
|
|
|
|
pa := &astruct{X: 6}
|
2018-08-18 08:53:21 +00:00
|
|
|
a2 := a2struct{Y: 7}
|
2019-06-17 16:51:29 +00:00
|
|
|
var pa2 *astruct
|
|
|
|
var str string = "old string value"
|
2018-07-31 12:50:10 +00:00
|
|
|
|
|
|
|
var vable_a VRcvrable = a
|
|
|
|
var vable_pa VRcvrable = pa
|
|
|
|
var pable_pa PRcvrable = pa
|
2020-02-18 22:19:06 +00:00
|
|
|
var x X = 2
|
|
|
|
var x2 X2 = 2
|
2018-07-31 12:50:10 +00:00
|
|
|
|
2018-07-31 16:32:30 +00:00
|
|
|
fn2clos := makeclos(pa)
|
|
|
|
fn2glob := call1
|
|
|
|
fn2valmeth := pa.VRcvr
|
|
|
|
fn2ptrmeth := pa.PRcvr
|
|
|
|
var fn2nil func()
|
|
|
|
|
2019-09-26 14:37:23 +00:00
|
|
|
d := &Derived{3, Base{4}}
|
|
|
|
|
2018-05-04 17:31:45 +00:00
|
|
|
runtime.Breakpoint()
|
|
|
|
call1(one, two)
|
2018-07-31 16:32:30 +00:00
|
|
|
fn2clos(2)
|
2019-09-25 17:23:02 +00:00
|
|
|
strings.LastIndexByte(stringslice[1], 'w')
|
2019-09-26 14:37:23 +00:00
|
|
|
d.Method()
|
|
|
|
d.Base.Method()
|
2020-02-18 22:19:06 +00:00
|
|
|
x.CallMe()
|
2020-11-12 23:24:31 +00:00
|
|
|
fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5))
|
2018-05-04 17:31:45 +00:00
|
|
|
}
|