Use variables from the Go standard library with Usestdlibvars open-source
Usestdlibvars linter determines the ability to reuse variables or constants from the standard library. So instead of writing your own constants, you can reuse those constants that are already in the standard library.
In Go development, developers often write their own variables or constants and then reuse them in code that accesses the standard library. That’s why our Go developer, Sasha Melentyev, created the usestdlibvars linter — which determines the ability to reuse variables or constants from the standard library. So instead of writing your own constants, you can reuse those constants that are already in the standard library.
For example, you need to make some kind of request. Generally speaking, it needs to use some method — like getpost — in which case, many developers write the word get directly. But instead, you can take and reuse a constant from the standard library. This will be even more visible, because it will be named accordingly, and will be called from the standard library. So, we can say that the linter saves time spent on writing extra code.
For instance, instead of http.NewRequest("GET", "", nil)
you can have http.NewRequest(http.MethodGet, "", nil)
— the linter will highlight it.
The fact that the standard library has a lot of different variables that can be reused, and they are not always needed, also plays an important role. According to the standard, only two options (for example, the check related to the http internal library) are included in our linter, so that there are no false positives.
The name of the linter, UseStdLibvars, is essentially a call for action. The linter is already included in golangci-lint.
Installation
go install github.com/sashamelentyev/usestdlibvars@latest
Usage
$ usestdlibvars -h
usestdlibvars: A linter that detect the possibility to use variables/constants from the Go standard library.
Usage: usestdlibvars [-flag] [package]
Flags:
-V print version and exit
-all
no effect (deprecated)
-c int
display offending line with this many lines of context (default -1)
-constant-kind
suggest the use of constant.Kind.String()
-cpuprofile string
write CPU profile to this file
-crypto-hash
suggest the use of crypto.Hash.String()
-debug string
debug flags, any subset of "fpstv"
-fix
apply all suggested fixes
-flags
print analyzer flags in JSON
-http-method
suggest the use of http.MethodXX (default true)
-http-status-code
suggest the use of http.StatusXX (default true)
-json
emit JSON output
-memprofile string
write memory profile to this file
-os-dev-null
suggest the use of os.DevNull
-rpc-default-path
suggest the use of rpc.DefaultXXPath
-source
no effect (deprecated)
-sql-isolation-level
suggest the use of sql.LevelXX.String()
-tags string
no effect (deprecated)
-test
indicates whether test files should be analyzed, too (default true)
-time-layout
suggest the use of time.Layout
-time-month
suggest the use of time.Month.String()
-time-weekday
suggest the use of time.Weekday.String()
-tls-signature-scheme
suggest the use of tls.SignatureScheme.String()
-trace string
write trace log to this file
-v no effect (deprecated)
Examples
package response
import (
"bytes"
"encoding/json"
"net/http"
)
// JSON marshals v to JSON, automatically escaping HTML,
// setting the Content-Type header as "application/json; charset=utf-8",
// sends an HTTP response header with the provided statusCode and
// writes the marshaled v as bytes to the connection as part of an HTTP reply.
func JSON(w http.ResponseWriter, statusCode int, v any) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(true)
if err := enc.Encode(v); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(statusCode)
if _, err := w.Write(buf.Bytes()); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
usestdlibvars ./...
response.go:18:30: "500" can be replaced by http.StatusInternalServerError
response.go:24:30: "500" can be replaced by http.StatusInternalServerError
Plans for the future
We plan to monitor what's new in the language. If there are any new constants that can be reused in some specific cases, then we will supplement the functionality of the linter. You are also welcome to contribute and send us pull requests.
If you want to learn more about using open-source solutions in your project, or need help developing your project from scratch, just send us a message using the form below. We will contact you to discuss the details and see how we can partner with you to develop your next great project together!