github.com/azazeal/revive@v1.0.9/testdata/golint/var-naming.go (about) 1 // Test for name linting. 2 3 // Package pkg_with_underscores ... 4 package pkg_with_underscores // MATCH /don't use an underscore in package name/ 5 6 import ( 7 "io" 8 "net" 9 net_http "net/http" // renamed deliberately 10 "net/url" 11 ) 12 13 import "C" 14 15 var safeUrl = "HttPS://..iaMHost..Test:443/paTh^A%ef//./%41PaTH/..//?" // MATCH /var safeUrl should be safeURL/ 16 var var_name int // MATCH /don't use underscores in Go names; var var_name should be varName/ 17 18 type t_wow struct { // MATCH /don't use underscores in Go names; type t_wow should be tWow/ 19 x_damn int // MATCH /don't use underscores in Go names; struct field x_damn should be xDamn/ 20 Url *url.URL // MATCH /struct field Url should be URL/ 21 } 22 23 const fooId = "blah" // MATCH /const fooId should be fooID/ 24 25 func f_it() { // MATCH /don't use underscores in Go names; func f_it should be fIt/ 26 more_underscore := 4 // MATCH /don't use underscores in Go names; var more_underscore should be moreUnderscore/ 27 _ = more_underscore 28 var err error 29 if isEof := (err == io.EOF); isEof { // MATCH /var isEof should be isEOF/ 30 more_underscore = 7 // should be okay 31 } 32 33 x := net_http.Request{} // should be okay 34 _ = x 35 36 var ips []net.IP 37 for _, theIp := range ips { // MATCH /range var theIp should be theIP/ 38 _ = theIp 39 } 40 41 switch myJson := g(); { // MATCH /var myJson should be myJSON/ 42 default: 43 _ = myJson 44 } 45 var y net_http.ResponseWriter // an interface 46 switch tApi := y.(type) { // MATCH /var tApi should be tAPI/ 47 default: 48 _ = tApi 49 } 50 51 var c chan int 52 select { 53 case qId := <-c: // MATCH /var qId should be qID/ 54 _ = qId 55 } 56 } 57 58 // Common styles in other languages that don't belong in Go. 59 const ( 60 CPP_CONST = 1 // MATCH /don't use ALL_CAPS in Go names; use CamelCase/ 61 kLeadingKay = 2 // MATCH /don't use leading k in Go names; const kLeadingKay should be leadingKay/ 62 63 HTML = 3 // okay; no underscore 64 X509B = 4 // ditto 65 ) 66 67 func f(bad_name int) {} // MATCH /don't use underscores in Go names; func parameter bad_name should be badName/ 68 func g() (no_way int) { return 0 } // MATCH /don't use underscores in Go names; func result no_way should be noWay/ 69 func (t *t_wow) f(more_under string) {} // MATCH /don't use underscores in Go names; method parameter more_under should be moreUnder/ 70 func (t *t_wow) g() (still_more string) { return "" } // MATCH /don't use underscores in Go names; method result still_more should be stillMore/ 71 72 type i interface { 73 CheckHtml() string // okay; interface method names are often constrained by the concrete types' method names 74 75 F(foo_bar int) // MATCH /don't use underscores in Go names; interface method parameter foo_bar should be fooBar/ 76 } 77 78 // All okay; underscore between digits 79 const case1_1 = 1 80 81 type case2_1 struct { 82 case2_2 int 83 } 84 85 func case3_1(case3_2 int) (case3_3 string) { 86 case3_4 := 4 87 _ = case3_4 88 89 return "" 90 } 91 92 type t struct{} 93 94 func (t) LastInsertId() (int64, error) { return 0, nil } // okay because it matches a known style violation 95 96 //export exported_to_c 97 func exported_to_c() {} // okay: https://github.com/golang/lint/issues/144 98 99 //export exported_to_c_with_arg 100 func exported_to_c_with_arg(but_use_go_param_names int) // MATCH /don't use underscores in Go names; func parameter but_use_go_param_names should be butUseGoParamNames/ 101 102 // This is an exported C function with a leading doc comment. 103 // 104 //export exported_to_c_with_comment 105 func exported_to_c_with_comment() {} // okay: https://github.com/golang/lint/issues/144 106 107 //export maybe_exported_to_CPlusPlusWithCamelCase 108 func maybe_exported_to_CPlusPlusWithCamelCase() {} // okay: https://github.com/golang/lint/issues/144 109 110 // WhyAreYouUsingCapitalLetters_InACFunctionName is a Go-exported function that 111 // is also exported to C as a name with underscores. 112 // 113 // Don't do that. If you want to use a C-style name for a C export, make it 114 // lower-case and leave it out of the Go-exported API. 115 // 116 //export WhyAreYouUsingCapitalLetters_InACFunctionName 117 func WhyAreYouUsingCapitalLetters_InACFunctionName() {} // MATCH /don't use underscores in Go names; func WhyAreYouUsingCapitalLetters_InACFunctionName should be WhyAreYouUsingCapitalLettersInACFunctionName/