Use variables from the Go standard library with Usestdlibvars open-source

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.


go install


$ usestdlibvars -h
usestdlibvars: A linter that detect the possibility to use variables/constants from the Go standard library.

Usage: usestdlibvars [-flag] [package]

  -V    print version and exit
        no effect (deprecated)
  -c int
        display offending line with this many lines of context (default -1)
        suggest the use of constant.Kind.String()
  -cpuprofile string
        write CPU profile to this file
        suggest the use of crypto.Hash.String()
  -debug string
        debug flags, any subset of "fpstv"
        apply all suggested fixes
        print analyzer flags in JSON
        suggest the use of http.MethodXX (default true)
        suggest the use of http.StatusXX (default true)
        emit JSON output
  -memprofile string
        write memory profile to this file
        suggest the use of os.DevNull
        suggest the use of rpc.DefaultXXPath
        no effect (deprecated)
        suggest the use of sql.LevelXX.String()
  -tags string
        no effect (deprecated)
        indicates whether test files should be analyzed, too (default true)
        suggest the use of time.Layout
        suggest the use of time.Month.String()
        suggest the use of time.Weekday.String()
        suggest the use of tls.SignatureScheme.String()
  -trace string
        write trace log to this file
  -v    no effect (deprecated)


package response

import (

// 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)
	if err := enc.Encode(v); err != nil {
		http.Error(w, err.Error(), 500)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	if _, err := w.Write(buf.Bytes()); err != nil {
		http.Error(w, err.Error(), 500)
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!

Linters help us not only avoid any vulnerabilities or reduced performance, but also keep the code base in the same style. Use linters!
Sasha Melentyev
Go developer,
Contact us
Have a project in mind?
Let's make it happen
Attach file
Files must be less than 8 MB.
Allowed file types: jpg jpeg png txt rtf pdf doc docx ppt pptx.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.