gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/golint/testdata/var-decl.go (about)

     1  // Test for redundant type declaration.
     2  
     3  // Package foo ...
     4  package foo
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"nosuchpkg" // export data unavailable
    11  	"os"
    12  )
    13  
    14  // Q is a test type.
    15  type Q bool
    16  
    17  var myInt int = 7                           // MATCH /should.*int.*myInt.*inferred/
    18  var mux *http.ServeMux = http.NewServeMux() // MATCH /should.*\*http\.ServeMux.*inferred/
    19  
    20  var myZeroInt int = 0         // MATCH /should.*= 0.*myZeroInt.*zero value/
    21  var myZeroFlt float32 = 0.    // MATCH /should.*= 0\..*myZeroFlt.*zero value/
    22  var myZeroF64 float64 = 0.0   // MATCH /should.*= 0\..*myZeroF64.*zero value/
    23  var myZeroImg complex64 = 0i  // MATCH /should.*= 0i.*myZeroImg.*zero value/
    24  var myZeroStr string = ""     // MATCH /should.*= "".*myZeroStr.*zero value/
    25  var myZeroRaw string = ``     // MATCH /should.*= ``.*myZeroRaw.*zero value/
    26  var myZeroPtr *Q = nil        // MATCH /should.*= nil.*myZeroPtr.*zero value/
    27  var myZeroRune rune = '\x00'  // MATCH /should.*= '\\x00'.*myZeroRune.*zero value/
    28  var myZeroRune2 rune = '\000' // MATCH /should.*= '\\000'.*myZeroRune2.*zero value/
    29  
    30  // No warning because there's no type on the LHS
    31  var x = 0
    32  
    33  // This shouldn't get a warning because there's no initial values.
    34  var str fmt.Stringer
    35  
    36  // No warning because this is a const.
    37  const k uint64 = 7
    38  
    39  const num = 123
    40  
    41  // No warning because the var's RHS is known to be an untyped const.
    42  var flags uint32 = num
    43  
    44  // No warnings because the RHS is an ideal int, and the LHS is a different int type.
    45  var userID int64 = 1235
    46  var negID int64 = -1
    47  var parenID int64 = (17)
    48  var crazyID int64 = -(-(-(-9)))
    49  
    50  // Same, but for strings and floats.
    51  type stringT string
    52  type floatT float64
    53  
    54  var stringV stringT = "abc"
    55  var floatV floatT = 123.45
    56  
    57  // No warning because the LHS names an interface type.
    58  var data interface{} = googleIPs
    59  var googleIPs []int
    60  
    61  // No warning because it's a common idiom for interface satisfaction.
    62  var _ Server = (*serverImpl)(nil)
    63  
    64  // Server is a test type.
    65  type Server interface{}
    66  type serverImpl struct{}
    67  
    68  // LHS is a different type than the RHS.
    69  var myStringer fmt.Stringer = q(0)
    70  
    71  // LHS is a different type than the RHS.
    72  var out io.Writer = os.Stdout
    73  
    74  var out2 io.Writer = newWriter() // MATCH /should omit.*io\.Writer/
    75  
    76  func newWriter() io.Writer { return nil }
    77  
    78  // We don't figure out the true types of LHS and RHS here,
    79  // so we suppress the check.
    80  var ni nosuchpkg.Interface = nosuchpkg.NewConcrete()
    81  
    82  var y string = q(1).String() // MATCH /should.*string/
    83  
    84  type q int
    85  
    86  func (q) String() string { return "I'm a q" }