Source file src/internal/types/testdata/check/compliterals1.go

     1  // Copyright 2026 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  // Composite literals with inferred types
     6  
     7  package comp_literals
     8  
     9  type T struct {}
    10  
    11  // var declaration
    12  func _() {
    13          var _ T = {}
    14  }
    15  
    16  // assignment
    17  func _() {
    18          var x T
    19          x = {}
    20          _ = x // "use" it
    21  }
    22  
    23  // // argument to function call
    24  // func _() {
    25  //         f := func(_ T) {}
    26  //         f({})
    27  // }
    28  
    29  // // argument to function call with variadic arguments
    30  // func _() {
    31  //         f := func(_ ...T) {}
    32  //         f({}, {})
    33  // }
    34  
    35  // map index expression
    36  func _() {
    37          var x map[T]int
    38          _ = x[{}]
    39  }
    40  
    41  // map index expression through a type parameter
    42  func _[P map[T]int](x P) {
    43          _ = x[{}]
    44  }
    45  
    46  // // value of a struct literal with keys / values
    47  // func _() {
    48  //         type S struct {
    49  //                 f T
    50  //         }
    51  //         _ = S{f: {}}
    52  // }
    53  
    54  // // value of a struct literal without keys / values
    55  // func _() {
    56  //         type S struct {
    57  //                 f T
    58  //         }
    59  //         _ = S{{}}
    60  // }
    61  
    62  // values sent to a channel
    63  func _() {
    64          var x chan<- T
    65          x <- {}
    66  }
    67  
    68  // argument to conversion
    69  func _() {
    70          _ = T({})
    71  }
    72  
    73  // The below are all covered by the inference mechanism used before
    74  // Go 1.28 (AKA "hints"). They are included here for completeness.
    75  
    76  // keys of a map literal
    77  func _() {
    78          type M map[T]int
    79          _ = M{{}: 42}
    80  }
    81  
    82  // values of a map literal
    83  func _() {
    84          type M map[int]T
    85          _ = M{42: {}}
    86  }
    87  
    88  // elements in an array literal
    89  func _() {
    90          _ = [42]T{{}}
    91  }
    92  
    93  // elements in a slice literal
    94  func _() {
    95          _ = []T{{}}
    96  }
    97  

View as plain text