• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// variable declarations
6
7package decls1
8
9import (
10	"math"
11)
12
13// Global variables without initialization
14var (
15	a, b bool
16	c byte
17	d uint8
18	r rune
19	i int
20	j, k, l int
21	x, y float32
22	xx, yy float64
23	u, v complex64
24	uu, vv complex128
25	s, t string
26	array []byte
27	iface interface{}
28
29	blank _ /* ERROR "cannot use _" */
30)
31
32// Global variables with initialization
33var (
34	s1 = i + j
35	s2 = i /* ERROR "mismatched types" */ + x
36	s3 = c + d
37	s4 = s + t
38	s5 = s /* ERROR "invalid operation" */ / t
39	s6 = array[t1]
40	s7 = array[x /* ERROR "integer" */]
41	s8 = &a
42	s10 = &42 /* ERROR "cannot take address" */
43	s11 = &v
44	s12 = -(u + *t11) / *&v
45	s13 = a /* ERROR "shifted operand" */ << d
46	s14 = i << j
47	s18 = math.Pi * 10.0
48	s19 = s1 /* ERROR "cannot call" */ ()
49 	s20 = f0 /* ERROR "no value" */ ()
50	s21 = f6(1, s1, i)
51	s22 = f6(1, s1, uu /* ERRORx `cannot use .* in argument` */ )
52
53	t1 int = i + j
54	t2 int = i /* ERROR "mismatched types" */ + x
55	t3 int = c /* ERRORx `cannot use .* variable declaration` */ + d
56	t4 string = s + t
57	t5 string = s /* ERROR "invalid operation" */ / t
58	t6 byte = array[t1]
59	t7 byte = array[x /* ERROR "must be integer" */]
60	t8 *int = & /* ERRORx `cannot use .* variable declaration` */ a
61	t10 *int = &42 /* ERROR "cannot take address" */
62	t11 *complex64 = &v
63	t12 complex64 = -(u + *t11) / *&v
64	t13 int = a /* ERROR "shifted operand" */ << d
65	t14 int = i << j
66	t15 math /* ERROR "math is not a type" */
67	t16 math.xxx /* ERROR "undefined" */
68	t17 math /* ERROR "not a type" */ .Pi
69	t18 float64 = math.Pi * 10.0
70	t19 int = t1 /* ERROR "cannot call" */ ()
71	t20 int = f0 /* ERROR "no value" */ ()
72	t21 int = a /* ERRORx `cannot use .* variable declaration` */
73)
74
75// Various more complex expressions
76var (
77	u1 = x /* ERROR "not an interface" */ .(int)
78	u2 = iface.([]int)
79	u3 = iface.(a /* ERROR "not a type" */ )
80	u4, ok = iface.(int)
81	u5, ok2, ok3 = iface /* ERROR "assignment mismatch" */ .(int)
82)
83
84// Constant expression initializations
85var (
86	v1 = 1 /* ERROR "mismatched types untyped int and untyped string" */ + "foo"
87	v2 = c + 255
88	v3 = c + 256 /* ERROR "overflows" */
89	v4 = r + 2147483647
90	v5 = r + 2147483648 /* ERROR "overflows" */
91	v6 = 42
92	v7 = v6 + 9223372036854775807
93	v8 = v6 + 9223372036854775808 /* ERROR "overflows" */
94	v9 = i + 1 << 10
95	v10 byte = 1024 /* ERROR "overflows" */
96	v11 = xx/yy*yy - xx
97	v12 = true && false
98	v13 = nil /* ERROR "use of untyped nil" */
99	v14 string = 257 // ERRORx `cannot use 257 .* as string value in variable declaration$`
100	v15 int8 = 257 // ERRORx `cannot use 257 .* as int8 value in variable declaration .*overflows`
101)
102
103// Multiple assignment expressions
104var (
105	m1a, m1b = 1, 2
106	m2a, m2b, m2c /* ERROR "missing init expr for m2c" */ = 1, 2
107	m3a, m3b = 1, 2, 3 /* ERROR "extra init expr 3" */
108)
109
110func _() {
111	var (
112		m1a, m1b = 1, 2
113		m2a, m2b, m2c /* ERROR "missing init expr for m2c" */ = 1, 2
114		m3a, m3b = 1, 2, 3 /* ERROR "extra init expr 3" */
115	)
116
117	_, _ = m1a, m1b
118	_, _, _ = m2a, m2b, m2c
119	_, _ = m3a, m3b
120}
121
122// Declaration of parameters and results
123func f0() {}
124func f1(a /* ERROR "not a type" */) {}
125func f2(a, b, c d /* ERROR "not a type" */) {}
126
127func f3() int { return 0 }
128func f4() a /* ERROR "not a type" */ { return 0 }
129func f5() (a, b, c d /* ERROR "not a type" */) { return }
130
131func f6(a, b, c int) complex128 { return 0 }
132
133// Declaration of receivers
134type T struct{}
135
136func (T) m0() {}
137func (*T) m1() {}
138func (x T) m2() {}
139func (x *T) m3() {}
140
141// Initialization functions
142func init() {}
143func init /* ERROR "no arguments and no return values" */ (int) {}
144func init /* ERROR "no arguments and no return values" */ () int { return 0 }
145func init /* ERROR "no arguments and no return values" */ (int) int { return 0 }
146func (T) init(int) int { return 0 }
147