{
"go.lintTool": "golangci-lint",
"go.formatTool": "goimports",
"go.useLanguageServer": true,
"go.lintOnSave": "package",
"go.vetOnSave": "package",
"go.vetFlags": [
"-all",
"-shadow"
]
}
Common Abbreviations Used In Go
Following are some of the common Abbreviations used in Go standard libraries:
var s string // string var i int // index var num int // number var msg string // message var v string // value var val string // value var fv string // flag value var err error // error value var args []string // arguments var seen bool // has seen? var parsed bool // parsing ok?
[Read More]
Constants in Go
Intro Constants are declared like variables, but with the const keyword. Constants can only be character, string, boolean, or numeric values and cannot be declared using the := syntax. An untyped constant takes the type needed by its context.
Example:
const Pi = 3.14 const ( StatusOK = 200 StatusCreated = 201 StatusAccepted = 202 StatusNonAuthoritativeInfo = 203 StatusNoContent = 204 StatusResetContent = 205 StatusPartialContent = 206 ) Constants belong to compile time.
[Read More]
Unused variables
Unused variables in blocked scope are not allowed in Go since they cause maintenance nightmares. If we declare a variable in blocked scope then we must use it or else completely remove it from the block. We cannot have unused variables declared in blocked scope dangling in our source codes. Go throws unused variable errors at compile time only. We should avoid using package level variables. Go doesn’t throw unused variable errors at compile time for variables declared at package level.
[Read More]
Use govendor to implement vendoring
The meaning of vendoring in Go is squeezing a project’s all dependencies into its vendor directory. Since Go 1.6, if there is a vendor directory in current package or its parent’s directory, the dependency will be searched in vendor directory first. Govendor is such a tool to help you make use of the vendor feature. In the following example, I will demonstrate how to use govendor step by step:
(1) To be more clear, I clean $GOPATH folder first:
[Read More]
Variables
In Go, we have to declare a variable before we can use it. This is required and necessary for the compile time safety. Variables are not created at compile time. They are created at run time. The unnamed variables are pointers (like in C). Once we declare a type for a variable, it cannot be changed later. It is static. Declaring with var The var statement declares a list of variables with the type declared last.
[Read More]