Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
563270ca02 | ||
|
|
5eecbe515b | ||
|
|
0a79069dfc | ||
|
|
0c8f538cd9 | ||
|
|
ca80ada849 | ||
|
|
3c6df504df | ||
|
|
98eacf3610 | ||
|
|
16ff52a949 | ||
|
|
640d1429e5 | ||
|
|
659913eebe | ||
|
|
b3766509cc | ||
|
|
bc2b224bae | ||
|
|
9d4685deea | ||
|
|
2a70a71dc2 | ||
|
|
851444453c | ||
|
|
a8b1c2a017 | ||
|
|
d229c2a2c7 | ||
|
|
2f2df7a0f8 | ||
|
|
4058fd8c44 | ||
|
|
097a745e72 | ||
|
|
1f514e63a8 | ||
|
|
a15ecb7176 | ||
|
|
d4aa84f729 | ||
|
|
9977ef6fc6 | ||
|
|
39430c34bb | ||
|
|
4f3481b55c | ||
|
|
55f2fe396a | ||
|
|
108b6fd722 | ||
|
|
a3b2737b5c | ||
|
|
d2c4a36c25 | ||
|
|
f5f44f7ddd | ||
|
|
4d013e4686 | ||
|
|
c11d361953 | ||
|
|
c2ad279643 | ||
|
|
9627782394 | ||
|
|
e00b853971 | ||
|
|
7cfa264dbc | ||
|
|
a6c24a0d13 | ||
|
|
f19b7563ea | ||
|
|
0643762852 | ||
|
|
7323d97023 | ||
|
|
6486909921 | ||
|
|
d252821df3 | ||
|
|
2bef03e253 | ||
|
|
1fe91be882 | ||
|
|
5cbbf9339c | ||
|
|
8365f687e7 | ||
|
|
36836cd4f2 | ||
|
|
82b499a1c8 | ||
|
|
0ef7f8fb80 | ||
|
|
c1f5005b2a | ||
|
|
def57d57c2 | ||
|
|
74479d70e6 | ||
|
|
f1cff308e6 | ||
|
|
6f878052f8 | ||
|
|
eb25c84797 | ||
|
|
2de0c80d38 | ||
|
|
151a856bf2 | ||
|
|
d5217f7db4 | ||
|
|
01e4cdea70 | ||
|
|
3c88542180 | ||
|
|
56d88ef89d | ||
|
|
5d78c8ae27 | ||
|
|
184623d81f | ||
|
|
5d56bac8d0 | ||
|
|
4a068ea452 | ||
|
|
8605c238ef | ||
|
|
4f39eaf893 | ||
|
|
4f8e1de267 | ||
|
|
6f4643ff19 | ||
|
|
bb1be9e6e1 | ||
|
|
cdf4622421 | ||
|
|
b9720d15e1 | ||
|
|
7fab75fbe4 | ||
|
|
7eac6955b3 |
@@ -38,6 +38,9 @@
|
||||
"gocognit",
|
||||
"stylecheck",
|
||||
"gomnd",
|
||||
"testpackage",
|
||||
"goerr113",
|
||||
"nestif",
|
||||
]
|
||||
|
||||
[issues]
|
||||
|
||||
@@ -39,7 +39,7 @@ install:
|
||||
- go mod download
|
||||
|
||||
before_script:
|
||||
- rm -f interp/op.go interp/interp_test.go
|
||||
- rm -f interp/op.go
|
||||
- make generate
|
||||
- git update-index -q --refresh
|
||||
- CHANGED=$(git diff-index --name-only HEAD --)
|
||||
|
||||
19
_test/assign13.go
Normal file
19
_test/assign13.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func getStr() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := make(map[string]string, 0)
|
||||
m["a"] = fmt.Sprintf("%v", 0.1)
|
||||
m["b"] = string(fmt.Sprintf("%v", 0.1))
|
||||
m["c"] = getStr()
|
||||
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[a:0.1 b:0.1 c:test]
|
||||
16
_test/assign14.go
Normal file
16
_test/assign14.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
var optionsG map[string]string = nil
|
||||
|
||||
var roundG = 30
|
||||
|
||||
func main() {
|
||||
dummy := roundG
|
||||
roundG = dummy + 1
|
||||
println(roundG)
|
||||
println(optionsG == nil)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 31
|
||||
// true
|
||||
11
_test/assign15.go
Normal file
11
_test/assign15.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var c chan<- struct{} = make(chan struct{})
|
||||
var d <-chan struct{} = c
|
||||
|
||||
_ = d
|
||||
}
|
||||
|
||||
// Error:
|
||||
// _test/assign15.go:5:26: cannot use type chan<- struct{} as type <-chan struct{} in assignment
|
||||
3
_test/baz-bat/baz-bat.go
Normal file
3
_test/baz-bat/baz-bat.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package baz
|
||||
|
||||
var Name = "baz-bat"
|
||||
12
_test/chan10.go
Normal file
12
_test/chan10.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
func main() {
|
||||
var tick <-chan time.Time = time.Tick(time.Millisecond)
|
||||
_ = tick
|
||||
println("success")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// success
|
||||
14
_test/comp2.go
Normal file
14
_test/comp2.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
type delta int32
|
||||
|
||||
func main() {
|
||||
a := delta(-1)
|
||||
|
||||
println(a != -1)
|
||||
println(a == -1)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
14
_test/composite10.go
Normal file
14
_test/composite10.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := []map[int]int{make(map[int]int)}
|
||||
|
||||
for _, b := range a {
|
||||
fmt.Println(b)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[]
|
||||
15
_test/composite11.go
Normal file
15
_test/composite11.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := color.NRGBA64{1, 1, 1, 1}
|
||||
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {1 1 1 1}
|
||||
14
_test/composite9.go
Normal file
14
_test/composite9.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := [][]int{make([]int,0)}
|
||||
|
||||
for _, b := range a {
|
||||
fmt.Println(b)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// []
|
||||
17
_test/const12.go
Normal file
17
_test/const12.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type Kind int
|
||||
|
||||
const (
|
||||
None Kind = 0
|
||||
Left Kind = 1 << iota
|
||||
Right
|
||||
Both Kind = Left | Right
|
||||
)
|
||||
|
||||
func main() {
|
||||
println(None, Left, Right, Both)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0 2 4 6
|
||||
16
_test/const13.go
Normal file
16
_test/const13.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const tooBig = 1267650600228229401496703205376
|
||||
const huge = 1 << 100
|
||||
const large = huge >> 38
|
||||
|
||||
fmt.Println(large)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 4611686018427387904
|
||||
13
_test/const14.go
Normal file
13
_test/const14.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "compress/flate"
|
||||
|
||||
func f1(i int) { println("i:", i) }
|
||||
|
||||
func main() {
|
||||
i := flate.BestSpeed
|
||||
f1(i)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// i: 1
|
||||
17
_test/const15.go
Normal file
17
_test/const15.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type T1 t1
|
||||
|
||||
type t1 int8
|
||||
|
||||
const (
|
||||
P2 T1 = 2
|
||||
P3 T1 = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
println(P3)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
32
_test/defer5.go
Normal file
32
_test/defer5.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
func f1() {
|
||||
defer println("f1-begin")
|
||||
f2()
|
||||
defer println("f1-end")
|
||||
}
|
||||
|
||||
func f2() {
|
||||
defer println("f2-begin")
|
||||
f3()
|
||||
defer println("f2-end")
|
||||
}
|
||||
|
||||
func f3() {
|
||||
defer println("f3-begin")
|
||||
println("hello")
|
||||
defer println("f3-end")
|
||||
}
|
||||
|
||||
func main() {
|
||||
f1()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello
|
||||
// f3-end
|
||||
// f3-begin
|
||||
// f2-end
|
||||
// f2-begin
|
||||
// f1-end
|
||||
// f1-begin
|
||||
27
_test/defer6.go
Normal file
27
_test/defer6.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
func f1() {
|
||||
defer print("f1-begin ")
|
||||
f2()
|
||||
defer print("f1-end ")
|
||||
}
|
||||
|
||||
func f2() {
|
||||
defer print("f2-begin ")
|
||||
f3()
|
||||
defer print("f2-end ")
|
||||
}
|
||||
|
||||
func f3() {
|
||||
defer print("f3-begin ")
|
||||
print("hello ")
|
||||
defer print("f3-end ")
|
||||
}
|
||||
|
||||
func main() {
|
||||
f1()
|
||||
println()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello f3-end f3-begin f2-end f2-begin f1-end f1-begin
|
||||
18
_test/defer7.go
Normal file
18
_test/defer7.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func f1(in, out []string) {
|
||||
defer copy(out, in)
|
||||
}
|
||||
|
||||
func main() {
|
||||
in := []string{"foo", "bar"}
|
||||
out := make([]string, 2)
|
||||
f1(in, out)
|
||||
|
||||
fmt.Println(out)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [foo bar]
|
||||
24
_test/defer8.go
Normal file
24
_test/defer8.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func f1(m map[string]string) {
|
||||
defer delete(m, "foo")
|
||||
defer delete(m, "test")
|
||||
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "bat",
|
||||
}
|
||||
f1(m)
|
||||
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[baz:bat foo:bar]
|
||||
// map[baz:bat]
|
||||
21
_test/defer9.go
Normal file
21
_test/defer9.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func f1(ch chan string) {
|
||||
defer close(ch)
|
||||
|
||||
ch <- "foo"
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := make(chan string, 1)
|
||||
f1(ch)
|
||||
|
||||
for s := range ch {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// foo
|
||||
17
_test/fun19.go
Normal file
17
_test/fun19.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func foo() ([]string, error) {
|
||||
return nil, fmt.Errorf("bar")
|
||||
}
|
||||
|
||||
func main() {
|
||||
a, b := foo()
|
||||
fmt.Println(a, b)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [] bar
|
||||
19
_test/fun20.go
Normal file
19
_test/fun20.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var myerr error = fmt.Errorf("bar")
|
||||
|
||||
func ferr() error { return myerr }
|
||||
|
||||
func foo() ([]string, error) {
|
||||
return nil, ferr()
|
||||
}
|
||||
|
||||
func main() {
|
||||
a, b := foo()
|
||||
fmt.Println(a, b)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [] bar
|
||||
12
_test/fun21.go
Normal file
12
_test/fun21.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
func Bar() string {
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
println(Bar())
|
||||
}
|
||||
|
||||
// Error:
|
||||
// 4:2: not enough arguments to return
|
||||
10
_test/fun22.go
Normal file
10
_test/fun22.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
func main() {
|
||||
time.Date()
|
||||
}
|
||||
|
||||
// Error:
|
||||
// 6:2: not enough arguments in call to time.Date
|
||||
10
_test/import9.go
Normal file
10
_test/import9.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "github.com/containous/yaegi/_test/baz-bat"
|
||||
|
||||
func main() {
|
||||
println(baz.Name)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// baz-bat
|
||||
12
_test/init1.go
Normal file
12
_test/init1.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
func init() {
|
||||
println("here")
|
||||
}
|
||||
|
||||
func main() {
|
||||
init()
|
||||
}
|
||||
|
||||
// Error:
|
||||
// _test/init1.go:8:2: undefined: init
|
||||
23
_test/interface37.go
Normal file
23
_test/interface37.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type I interface {
|
||||
A() string
|
||||
B() string
|
||||
}
|
||||
|
||||
type s struct{}
|
||||
|
||||
func NewS() (I, error) {
|
||||
return &s{}, nil
|
||||
}
|
||||
|
||||
func (c *s) A() string { return "a" }
|
||||
func (c *s) B() string { return "b" }
|
||||
|
||||
func main() {
|
||||
s, _ := NewS()
|
||||
println(s.A())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// a
|
||||
19
_test/interface38.go
Normal file
19
_test/interface38.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f foo) String() string {
|
||||
return "Hello from " + f.bar
|
||||
}
|
||||
|
||||
func main() {
|
||||
var f fmt.Stringer = foo{bar: "bar"}
|
||||
fmt.Println(f)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from bar
|
||||
19
_test/interface39.go
Normal file
19
_test/interface39.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f *foo) String() string {
|
||||
return "Hello from " + f.bar
|
||||
}
|
||||
|
||||
func main() {
|
||||
var f fmt.Stringer = &foo{bar: "bar"}
|
||||
fmt.Println(f)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from bar
|
||||
23
_test/interface40.go
Normal file
23
_test/interface40.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f foo) String() string {
|
||||
return "Hello from " + f.bar
|
||||
}
|
||||
|
||||
func Foo(s string) fmt.Stringer {
|
||||
return foo{s}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := Foo("bar")
|
||||
fmt.Println(f)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from bar
|
||||
23
_test/interface41.go
Normal file
23
_test/interface41.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f *foo) String() string {
|
||||
return "Hello from " + f.bar
|
||||
}
|
||||
|
||||
func Foo(s string) fmt.Stringer {
|
||||
return &foo{s}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := Foo("bar")
|
||||
fmt.Println(f)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from bar
|
||||
12
_test/interface42.go
Normal file
12
_test/interface42.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
v := interface{}(0)
|
||||
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0
|
||||
12
_test/interface43.go
Normal file
12
_test/interface43.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
v := interface{}(nil)
|
||||
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
19
_test/interface44.go
Normal file
19
_test/interface44.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
type S struct {
|
||||
a int
|
||||
}
|
||||
|
||||
func main() {
|
||||
var i interface{} = S{a: 1}
|
||||
|
||||
s, ok := i.(S)
|
||||
if !ok {
|
||||
println("bad")
|
||||
return
|
||||
}
|
||||
println(s.a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
13
_test/interface45.go
Normal file
13
_test/interface45.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var i interface{} = 1
|
||||
var s struct{}
|
||||
s, _ = i.(struct{})
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {}
|
||||
29
_test/issue-735.go
Normal file
29
_test/issue-735.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var optionsG map[string]string
|
||||
|
||||
var roundG int = 30
|
||||
|
||||
func strToInt(s string, defaultValue int) int {
|
||||
n, err := strconv.ParseInt(s, 10, 0)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return int(n)
|
||||
}
|
||||
|
||||
func main() {
|
||||
optionsG := map[string]string{"round": "12", "b": "one"}
|
||||
roundG = strToInt(optionsG["round"], 50)
|
||||
fmt.Println(roundG)
|
||||
fmt.Println(optionsG)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 12
|
||||
// map[b:one round:12]
|
||||
31
_test/issue-772.go
Normal file
31
_test/issue-772.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func main() {
|
||||
tmpl := template.New("name")
|
||||
|
||||
_, err := tmpl.Parse("{{.Data}}")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = tmpl.Execute(os.Stdout, Message{
|
||||
Data: "Hello, World!!",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello, World!!
|
||||
18
_test/issue-775.go
Normal file
18
_test/issue-775.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
recorder := httptest.NewRecorder()
|
||||
recorder.Header().Add("Foo", "Bar")
|
||||
|
||||
for key, value := range recorder.Header() {
|
||||
fmt.Println(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Foo [Bar]
|
||||
39
_test/issue-776.go
Normal file
39
_test/issue-776.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Filter is a filter
|
||||
type Filter interface {
|
||||
Foo()
|
||||
}
|
||||
|
||||
// GIFT is a gift
|
||||
type GIFT struct {
|
||||
Filters []Filter
|
||||
}
|
||||
|
||||
// New is a new filter list
|
||||
func New(filters ...Filter) *GIFT {
|
||||
return &GIFT{
|
||||
Filters: filters,
|
||||
}
|
||||
}
|
||||
|
||||
// List lists filters
|
||||
func (g *GIFT) List() {
|
||||
fmt.Printf("Hello from List!\n")
|
||||
}
|
||||
|
||||
// MyFilter is one of the filters
|
||||
type MyFilter struct{}
|
||||
|
||||
// Foo is a foo
|
||||
func (f *MyFilter) Foo() {}
|
||||
|
||||
func main() {
|
||||
g := New(&MyFilter{})
|
||||
g.List()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from List!
|
||||
12
_test/make2.go
Normal file
12
_test/make2.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var s uint = 4
|
||||
t := make([]int, s)
|
||||
fmt.Println(t)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [0 0 0 0]
|
||||
24
_test/map27.go
Normal file
24
_test/map27.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type fm map[string]interface{}
|
||||
|
||||
type foo struct{}
|
||||
|
||||
func main() {
|
||||
a := make(fm)
|
||||
a["foo"] = &foo{}
|
||||
fmt.Println(a["foo"])
|
||||
|
||||
b := make(template.FuncMap) // type FuncMap map[string]interface{}
|
||||
b["foo"] = &foo{}
|
||||
fmt.Println(b["foo"])
|
||||
}
|
||||
|
||||
// Output:
|
||||
// &{}
|
||||
// &{}
|
||||
21
_test/map28.go
Normal file
21
_test/map28.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
value1 := url.Values{}
|
||||
|
||||
value1.Set("first", "v1")
|
||||
value1.Set("second", "v2")
|
||||
|
||||
l := 0
|
||||
for k, v := range value1 {
|
||||
l += len(k) + len(v)
|
||||
}
|
||||
println(l)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 13
|
||||
26
_test/map29.go
Normal file
26
_test/map29.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
Object interface{}
|
||||
Expiry time.Duration
|
||||
}
|
||||
|
||||
func main() {
|
||||
items := map[string]Item{}
|
||||
|
||||
items["test"] = Item{
|
||||
Object: "test",
|
||||
Expiry: time.Second,
|
||||
}
|
||||
|
||||
item := items["test"]
|
||||
fmt.Println(item)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {test 1s}
|
||||
12
_test/math2.go
Normal file
12
_test/math2.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
const c uint64 = 2
|
||||
|
||||
func main() {
|
||||
if c&(1<<(uint64(1))) > 0 {
|
||||
println("yes")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// yes
|
||||
18
_test/method32.go
Normal file
18
_test/method32.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var a = []func(string){bar}
|
||||
b := a[0]
|
||||
b("bar")
|
||||
}
|
||||
|
||||
func bar(a string) {
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bar
|
||||
58
_test/method33.go
Normal file
58
_test/method33.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type T1 struct{}
|
||||
|
||||
func (t1 T1) f() {
|
||||
fmt.Println("T1.f()")
|
||||
}
|
||||
|
||||
func (t1 T1) g() {
|
||||
fmt.Println("T1.g()")
|
||||
}
|
||||
|
||||
type T2 struct {
|
||||
T1
|
||||
}
|
||||
|
||||
func (t2 T2) f() {
|
||||
fmt.Println("T2.f()")
|
||||
}
|
||||
|
||||
type I interface {
|
||||
f()
|
||||
}
|
||||
|
||||
func printType(i I) {
|
||||
if t1, ok := i.(T1); ok {
|
||||
println("T1 ok")
|
||||
t1.f()
|
||||
t1.g()
|
||||
}
|
||||
|
||||
if t2, ok := i.(T2); ok {
|
||||
println("T2 ok")
|
||||
t2.f()
|
||||
t2.g()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
println("T1")
|
||||
printType(T1{})
|
||||
println("T2")
|
||||
printType(T2{})
|
||||
}
|
||||
|
||||
// Output:
|
||||
// T1
|
||||
// T1 ok
|
||||
// T1.f()
|
||||
// T1.g()
|
||||
// T2
|
||||
// T2 ok
|
||||
// T2.f()
|
||||
// T1.g()
|
||||
23
_test/method34.go
Normal file
23
_test/method34.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type Root struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type One struct {
|
||||
Root
|
||||
}
|
||||
|
||||
type Hi interface {
|
||||
Hello() string
|
||||
}
|
||||
|
||||
func (r *Root) Hello() string { return "Hello " + r.Name }
|
||||
|
||||
func main() {
|
||||
var one interface{} = &One{Root{Name: "test2"}}
|
||||
println(one.(Hi).Hello())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello test2
|
||||
17
_test/op6.go
Normal file
17
_test/op6.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type T int
|
||||
|
||||
func (t T) Error() string { return "T: error" }
|
||||
|
||||
var invalidT T
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
if err != invalidT {
|
||||
println("ok")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// ok
|
||||
17
_test/op7.go
Normal file
17
_test/op7.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type T int
|
||||
|
||||
func (t T) Error() string { return "T: error" }
|
||||
|
||||
var invalidT T
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
if err > invalidT {
|
||||
println("ok")
|
||||
}
|
||||
}
|
||||
|
||||
// Error:
|
||||
// _test/op7.go:11:5: invalid operation: operator > not defined on error
|
||||
21
_test/op8.go
Normal file
21
_test/op8.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
type I interface {
|
||||
Get() interface{}
|
||||
}
|
||||
|
||||
type T struct{}
|
||||
|
||||
func (T) Get() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var i I = T{}
|
||||
var ei interface{}
|
||||
|
||||
println(i != ei)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
11
_test/op9.go
Normal file
11
_test/op9.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var i complex128 = 1i
|
||||
var f complex128 = 0.4i
|
||||
|
||||
print(i > f)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// _test/op9.go:7:8: invalid operation: operator > not defined on complex128
|
||||
21
_test/range7.go
Normal file
21
_test/range7.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func someChan() <-chan struct{} {
|
||||
c := make(chan struct{}, 1)
|
||||
c <- struct{}{}
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
for _ = range someChan() {
|
||||
fmt.Println("success")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// success
|
||||
16
_test/range8.go
Normal file
16
_test/range8.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for _ = range time.Tick(time.Millisecond) {
|
||||
fmt.Println("success")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// success
|
||||
11
_test/range9.go
Normal file
11
_test/range9.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var c chan<- struct{} = make(chan struct{})
|
||||
|
||||
for _ = range c {
|
||||
}
|
||||
}
|
||||
|
||||
// Error:
|
||||
// _test/range9.go:6:16: invalid operation: range c receive from send-only channel
|
||||
15
_test/recover2.go
Normal file
15
_test/recover2.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
println("hello")
|
||||
|
||||
var r interface{} = 1
|
||||
r = recover()
|
||||
if r == nil {
|
||||
println("world")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello
|
||||
// world
|
||||
22
_test/recover3.go
Normal file
22
_test/recover3.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
println("hello")
|
||||
|
||||
var r interface{} = 1
|
||||
r = recover()
|
||||
fmt.Printf("%v\n", r)
|
||||
if r == nil {
|
||||
println("world")
|
||||
}
|
||||
if r != nil {
|
||||
println("exception")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello
|
||||
// <nil>
|
||||
// world
|
||||
25
_test/recover4.go
Normal file
25
_test/recover4.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func div(a, b int) (result int) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
|
||||
fmt.Printf("r = %#v\n", r)
|
||||
|
||||
if r != nil {
|
||||
result = 0
|
||||
}
|
||||
}()
|
||||
|
||||
return a / b
|
||||
}
|
||||
|
||||
func main() {
|
||||
println(div(30, 2))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// r = <nil>
|
||||
// 15
|
||||
14
_test/redeclaration-global0.go
Normal file
14
_test/redeclaration-global0.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
type time int
|
||||
|
||||
var time string
|
||||
|
||||
func main() {
|
||||
time = "hello"
|
||||
println(time)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global0.go:5:5: time redeclared in this block
|
||||
// previous declaration at ../_test/redeclaration-global0.go:3:6
|
||||
12
_test/redeclaration-global1.go
Normal file
12
_test/redeclaration-global1.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
var time int
|
||||
|
||||
type time string
|
||||
|
||||
func main() {
|
||||
var t time = "hello"
|
||||
println(t)
|
||||
}
|
||||
|
||||
// TODO: expected redeclaration error.
|
||||
14
_test/redeclaration-global2.go
Normal file
14
_test/redeclaration-global2.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var t time.Time
|
||||
println(t.String())
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global2.go:5:2: time/redeclaration-global2.go redeclared in this block
|
||||
15
_test/redeclaration-global3.go
Normal file
15
_test/redeclaration-global3.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var time string
|
||||
|
||||
func main() {
|
||||
time = "hello"
|
||||
println(t)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global3.go:7:5: time redeclared in this block
|
||||
15
_test/redeclaration-global4.go
Normal file
15
_test/redeclaration-global4.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type time string
|
||||
|
||||
func main() {
|
||||
var t time = "hello"
|
||||
println(t)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global4.go:7:6: time redeclared in this block
|
||||
15
_test/redeclaration-global5.go
Normal file
15
_test/redeclaration-global5.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
var time int
|
||||
|
||||
func time() string {
|
||||
return "hello"
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := time()
|
||||
println(t)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global5.go:5:1: time redeclared in this block
|
||||
17
_test/redeclaration-global6.go
Normal file
17
_test/redeclaration-global6.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func time() string {
|
||||
return "hello"
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := time()
|
||||
println(t)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration-global6.go:7:1: time redeclared in this block
|
||||
15
_test/redeclaration0.go
Normal file
15
_test/redeclaration0.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
type foo struct {
|
||||
yolo string
|
||||
}
|
||||
|
||||
var foo int
|
||||
foo = 2
|
||||
println(foo)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration0.go:8:6: foo redeclared in this block
|
||||
// previous declaration at ../_test/redeclaration0.go:4:7
|
||||
12
_test/redeclaration1.go
Normal file
12
_test/redeclaration1.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var foo string
|
||||
|
||||
var foo int
|
||||
foo = 2
|
||||
println(foo)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration1.go:6:6: foo redeclared in this block
|
||||
15
_test/redeclaration2.go
Normal file
15
_test/redeclaration2.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var foo struct {
|
||||
yolo string
|
||||
}
|
||||
|
||||
var foo int
|
||||
foo = 2
|
||||
println(foo)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration2.go:8:6: foo redeclared in this block
|
||||
// previous declaration at ../_test/redeclaration2.go:4:6
|
||||
13
_test/redeclaration3.go
Normal file
13
_test/redeclaration3.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var foo int
|
||||
foo = 2
|
||||
|
||||
type foo struct{}
|
||||
var bar foo
|
||||
println(bar)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration3.go:7:7: foo redeclared in this block
|
||||
14
_test/redeclaration4.go
Normal file
14
_test/redeclaration4.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var foo struct{
|
||||
yolo string
|
||||
}
|
||||
|
||||
type foo struct{}
|
||||
var bar foo
|
||||
println(bar)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration4.go:8:7: foo redeclared in this block
|
||||
14
_test/redeclaration5.go
Normal file
14
_test/redeclaration5.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
type foo struct{
|
||||
yolo string
|
||||
}
|
||||
|
||||
type foo struct{}
|
||||
var bar foo
|
||||
println(bar)
|
||||
}
|
||||
|
||||
// Error:
|
||||
// ../_test/redeclaration5.go:8:7: foo redeclared in this block
|
||||
18
_test/restricted0.go
Normal file
18
_test/restricted0.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println("recover:", r)
|
||||
}()
|
||||
log.Fatal("log.Fatal does not exit")
|
||||
println("not printed")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// recover: log.Fatal does not exit
|
||||
18
_test/restricted1.go
Normal file
18
_test/restricted1.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println("recover:", r)
|
||||
}()
|
||||
os.Exit(1)
|
||||
println("not printed")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// recover: os.Exit(1)
|
||||
14
_test/restricted2.go
Normal file
14
_test/restricted2.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p, err := os.FindProcess(os.Getpid())
|
||||
fmt.Println(p, err)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <nil> restricted
|
||||
23
_test/restricted3.go
Normal file
23
_test/restricted3.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
buf bytes.Buffer
|
||||
logger = log.New(&buf, "logger: ", log.Lshortfile)
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println("recover:", r, buf.String())
|
||||
}()
|
||||
logger.Fatal("test log")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// recover: test log logger: restricted.go:39: test log
|
||||
23
_test/select12.go
Normal file
23
_test/select12.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type S struct {
|
||||
q chan struct{}
|
||||
}
|
||||
|
||||
func (s *S) Send() {
|
||||
select {
|
||||
case s.q <- struct{}{}:
|
||||
println("sent")
|
||||
default:
|
||||
println("unexpected")
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
s := &S{q: make(chan struct{}, 1)}
|
||||
s.Send()
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// sent
|
||||
// bye
|
||||
16
_test/select13.go
Normal file
16
_test/select13.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var c interface{} = int64(1)
|
||||
q := make(chan struct{})
|
||||
select {
|
||||
case q <- struct{}{}:
|
||||
println("unexpected")
|
||||
default:
|
||||
_ = c.(int64)
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bye
|
||||
18
_test/selector-scope0.go
Normal file
18
_test/selector-scope0.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func test(time string, t time.Time) string {
|
||||
return time
|
||||
}
|
||||
|
||||
func main() {
|
||||
str := test("test", time.Now())
|
||||
fmt.Println(str)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test
|
||||
@@ -1,15 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type S struct {
|
||||
Child []*S
|
||||
Name string
|
||||
Child []*S
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := &S{Name: "root"}
|
||||
s.Child = append(s.Child, &S{Name: "child"})
|
||||
println(s.Child[0].Name)
|
||||
a := S{Name: "hello"}
|
||||
a.Child = append(a.Child, &S{Name: "world"})
|
||||
json.NewEncoder(os.Stdout).Encode(a)
|
||||
a.Child[0].Child = append([]*S{}, &S{Name: "sunshine"})
|
||||
json.NewEncoder(os.Stdout).Encode(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// child
|
||||
// {"Name":"hello","Child":[{"Name":"world","Child":null}]}
|
||||
// {"Name":"hello","Child":[{"Name":"world","Child":[{"Name":"sunshine","Child":null}]}]}
|
||||
|
||||
21
_test/struct46.go
Normal file
21
_test/struct46.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type A struct {
|
||||
B string
|
||||
C D
|
||||
}
|
||||
|
||||
type D struct {
|
||||
E *A
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := &A{B: "b"}
|
||||
a.C = D{E: a}
|
||||
fmt.Println(a.C.E.B)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// b
|
||||
26
_test/struct47.go
Normal file
26
_test/struct47.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type A struct {
|
||||
B string
|
||||
C D
|
||||
}
|
||||
|
||||
func (a *A) Test() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
type D struct {
|
||||
E *A
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := &A{B: "b"}
|
||||
d := D{E: a}
|
||||
a.C = d
|
||||
fmt.Println(a.C.E.Test())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test
|
||||
37
_test/struct48.go
Normal file
37
_test/struct48.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
type List struct {
|
||||
Next *List
|
||||
Num int
|
||||
}
|
||||
|
||||
func add(l *List, n int) *List {
|
||||
if l == nil {
|
||||
return &List{Num: n}
|
||||
}
|
||||
l.Next = add(l.Next, n)
|
||||
return l
|
||||
}
|
||||
|
||||
func pr(l *List) {
|
||||
if l == nil {
|
||||
println("")
|
||||
return
|
||||
}
|
||||
print(l.Num)
|
||||
pr(l.Next)
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := add(nil, 0)
|
||||
pr(a)
|
||||
a = add(a, 1)
|
||||
pr(a)
|
||||
a = add(a, 2)
|
||||
pr(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0
|
||||
// 01
|
||||
// 012
|
||||
33
_test/struct49.go
Normal file
33
_test/struct49.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
type S struct {
|
||||
ts map[string][]*T
|
||||
}
|
||||
|
||||
type T struct {
|
||||
s *S
|
||||
}
|
||||
|
||||
func (c *S) getT(addr string) (t *T, ok bool) {
|
||||
cns, ok := c.ts[addr]
|
||||
if !ok || len(cns) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
t = cns[len(cns)-1]
|
||||
c.ts[addr] = cns[:len(cns)-1]
|
||||
return t, true
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := &S{
|
||||
ts: map[string][]*T{},
|
||||
}
|
||||
s.ts["test"] = append(s.ts["test"], &T{s: s})
|
||||
|
||||
t , ok:= s.getT("test")
|
||||
println(t != nil, ok)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true true
|
||||
20
_test/struct50.go
Normal file
20
_test/struct50.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Child []Node
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Node{Name: "hello"}
|
||||
a.Child = append([]Node{}, Node{Name: "world"})
|
||||
fmt.Println(a)
|
||||
a.Child[0].Child = append([]Node{}, Node{Name: "sunshine"})
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {hello [{world []}]}
|
||||
// {hello [{world [{sunshine []}]}]}
|
||||
23
_test/struct51.go
Normal file
23
_test/struct51.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Child [2]*Node
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Node{Name: "hello"}
|
||||
a.Child[0] = &Node{Name: "world"}
|
||||
json.NewEncoder(os.Stdout).Encode(a)
|
||||
a.Child[0].Child[0] = &Node{Name: "sunshine"}
|
||||
json.NewEncoder(os.Stdout).Encode(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {"Name":"hello","Child":[{"Name":"world","Child":[null,null]},null]}
|
||||
// {"Name":"hello","Child":[{"Name":"world","Child":[{"Name":"sunshine","Child":[null,null]},null]},null]}
|
||||
20
_test/struct52.go
Normal file
20
_test/struct52.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Child map[string]Node
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Node{Name: "hello", Child: map[string]Node{}}
|
||||
a.Child["1"] = Node{Name: "world", Child: map[string]Node{}}
|
||||
fmt.Println(a)
|
||||
a.Child["1"].Child["1"] = Node{Name: "sunshine", Child: map[string]Node{}}
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {hello map[1:{world map[]}]}
|
||||
// {hello map[1:{world map[1:{sunshine map[]}]}]}
|
||||
23
_test/struct53.go
Normal file
23
_test/struct53.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type T1 struct {
|
||||
P []*T
|
||||
}
|
||||
|
||||
type T2 struct {
|
||||
P2 *T
|
||||
}
|
||||
|
||||
type T struct {
|
||||
*T1
|
||||
S1 *T
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(T2{})
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {<nil>}
|
||||
26
_test/struct54.go
Normal file
26
_test/struct54.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
type S struct {
|
||||
t *T
|
||||
}
|
||||
|
||||
func newS() *S {
|
||||
return &S{
|
||||
t: &T{u: map[string]*U{}},
|
||||
}
|
||||
}
|
||||
|
||||
type T struct {
|
||||
u map[string]*U
|
||||
}
|
||||
|
||||
type U struct {
|
||||
a int
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := newS()
|
||||
_ = s
|
||||
|
||||
println("ok")
|
||||
}
|
||||
17
_test/switch35.go
Normal file
17
_test/switch35.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
a := 2
|
||||
switch {
|
||||
case a == 1:
|
||||
println(1)
|
||||
case a == 2:
|
||||
println(2)
|
||||
default:
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
// bye
|
||||
14
_test/switch36.go
Normal file
14
_test/switch36.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
a := 2
|
||||
switch {
|
||||
case a == 1:
|
||||
println(1)
|
||||
case a == 2:
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bye
|
||||
15
_test/switch37.go
Normal file
15
_test/switch37.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
a := 2
|
||||
switch {
|
||||
case a == 1:
|
||||
println(1)
|
||||
case a == 3:
|
||||
default:
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bye
|
||||
28
_test/switch38.go
Normal file
28
_test/switch38.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
func isSeparator(c byte) bool {
|
||||
switch c {
|
||||
case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := "max-age=20"
|
||||
for _, c := range []byte(s) {
|
||||
println(string(c), isSeparator(c))
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// m false
|
||||
// a false
|
||||
// x false
|
||||
// - false
|
||||
// a false
|
||||
// g false
|
||||
// e false
|
||||
// = true
|
||||
// 2 false
|
||||
// 0 false
|
||||
18
_test/time13.go
Normal file
18
_test/time13.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dummy = 1
|
||||
|
||||
var t time.Time = time.Date(2007, time.November, 10, 23, 4, 5, 0, time.UTC)
|
||||
|
||||
func main() {
|
||||
t = time.Date(2009, time.November, 10, 23, 4, 5, 0, time.UTC)
|
||||
fmt.Println(t.Clock())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 23 4 5
|
||||
38
_test/type23.go
Normal file
38
_test/type23.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var v1 interface{} = 1
|
||||
var v2 interface{}
|
||||
var v3 http.ResponseWriter = httptest.NewRecorder()
|
||||
|
||||
if r1, ok := v1.(string); ok {
|
||||
_ = r1
|
||||
println("unexpected")
|
||||
}
|
||||
if _, ok := v1.(string); ok {
|
||||
println("unexpected")
|
||||
}
|
||||
if r2, ok := v2.(string); ok {
|
||||
_ = r2
|
||||
println("unexpected")
|
||||
}
|
||||
if _, ok := v2.(string); ok {
|
||||
println("unexpected")
|
||||
}
|
||||
if r3, ok := v3.(http.Pusher); ok {
|
||||
_ = r3
|
||||
println("unexpected")
|
||||
}
|
||||
if _, ok := v3.(http.Pusher); ok {
|
||||
println("unexpected")
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bye
|
||||
48
_test/type24.go
Normal file
48
_test/type24.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
assertInt()
|
||||
assertNil()
|
||||
assertValue()
|
||||
}
|
||||
|
||||
func assertInt() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println(r)
|
||||
}()
|
||||
|
||||
var v interface{} = 1
|
||||
println(v.(string))
|
||||
}
|
||||
|
||||
func assertNil() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println(r)
|
||||
}()
|
||||
|
||||
var v interface{}
|
||||
println(v.(string))
|
||||
}
|
||||
|
||||
func assertValue() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
fmt.Println(r)
|
||||
}()
|
||||
|
||||
var v http.ResponseWriter = httptest.NewRecorder()
|
||||
println(v.(http.Pusher))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// interface conversion: interface {} is int, not string
|
||||
// interface conversion: interface {} is nil, not string
|
||||
// interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push
|
||||
44
_test/type25.go
Normal file
44
_test/type25.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type wrappedError struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (e wrappedError) Error() string {
|
||||
return "some outer error"
|
||||
}
|
||||
|
||||
func (e wrappedError) Unwrap() error {
|
||||
return e.wrapped
|
||||
}
|
||||
|
||||
var err atomic.Value
|
||||
|
||||
func getWrapped() *wrappedError {
|
||||
if v := err.Load(); v != nil {
|
||||
err := v.(wrappedError)
|
||||
if err.wrapped != nil {
|
||||
return &err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err.Store(wrappedError{wrapped: errors.New("test")})
|
||||
|
||||
e := getWrapped()
|
||||
if e != nil {
|
||||
println(e.Error())
|
||||
println(e.wrapped.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// some outer error
|
||||
// test
|
||||
44
_test/type26.go
Normal file
44
_test/type26.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type wrappedError struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
func (e *wrappedError) Error() string {
|
||||
return "some outer error"
|
||||
}
|
||||
|
||||
func (e *wrappedError) Unwrap() error {
|
||||
return e.wrapped
|
||||
}
|
||||
|
||||
var err atomic.Value
|
||||
|
||||
func getWrapped() *wrappedError {
|
||||
if v := err.Load(); v != nil {
|
||||
err := v.(*wrappedError)
|
||||
if err.wrapped != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err.Store(&wrappedError{wrapped: errors.New("test")})
|
||||
|
||||
e := getWrapped()
|
||||
if e != nil {
|
||||
println(e.Error())
|
||||
println(e.wrapped.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// some outer error
|
||||
// test
|
||||
15
_test/unsafe0.go
Normal file
15
_test/unsafe0.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "unsafe"
|
||||
|
||||
func main() {
|
||||
str := "foobar"
|
||||
|
||||
p := unsafe.Pointer(&str)
|
||||
str2 := *(*string)(p)
|
||||
|
||||
println(str2)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// foobar
|
||||
20
_test/unsafe1.go
Normal file
20
_test/unsafe1.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "unsafe"
|
||||
|
||||
type S struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := &S{Name: "foobar"}
|
||||
|
||||
p := unsafe.Pointer(s)
|
||||
|
||||
s2 := (*S)(p)
|
||||
|
||||
println(s2.Name)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// foobar
|
||||
20
_test/unsafe2.go
Normal file
20
_test/unsafe2.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
str := "foobar"
|
||||
|
||||
ptr := unsafe.Pointer(&str)
|
||||
p := uintptr(ptr)
|
||||
|
||||
s1 := fmt.Sprintf("%x", ptr)
|
||||
s2 := fmt.Sprintf("%x", p)
|
||||
println(s1 == s2)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
26
_test/unsafe3.go
Normal file
26
_test/unsafe3.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const SSize = 16
|
||||
|
||||
type S struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func main() {
|
||||
var sBuf [SSize]byte
|
||||
s := (*S)(unsafe.Pointer(&sBuf[0]))
|
||||
|
||||
s.X = 2
|
||||
s.Y = 4
|
||||
|
||||
fmt.Println(sBuf)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [2 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user