github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/test/initializerr.go (about) 1 // errorcheck 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Verify that erroneous initialization expressions are caught by the compiler 8 // Does not compile. 9 10 package main 11 12 type S struct { 13 A, B, C, X, Y, Z int 14 } 15 16 type T struct { 17 S 18 } 19 20 var x = 1 21 var a1 = S { 0, X: 1 } // ERROR "mixture|undefined" 22 var a2 = S { Y: 3, Z: 2, Y: 3 } // ERROR "duplicate" 23 var a3 = T { S{}, 2, 3, 4, 5, 6 } // ERROR "convert|too many" 24 var a4 = [5]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // ERROR "index|too many" 25 var a5 = []byte { x: 2 } // ERROR "index" 26 27 var ok1 = S { } // should be ok 28 var ok2 = T { S: ok1 } // should be ok 29 30 // These keys can be computed at compile time but they are 31 // not constants as defined by the spec, so they do not trigger 32 // compile-time errors about duplicate key values. 33 // See issue 4555. 34 35 type Key struct {X, Y int} 36 37 var _ = map[Key]string{ 38 Key{1,2}: "hello", 39 Key{1,2}: "world", 40 }