Variables in Go
Go: Variables
In Go, variables are explicitly declared and used by the compiler.
var
can declares one or more variables, you can declare multiple variables at once.
var a = "initial"
fmt.Println(a)
var b, c int = 1, 2
fmt.Println(b, c)
- Go will infer the type of initialized variables.
var d = true
fmt.Println(d)
- Variables declared without a corresponding initialization are zero-valued. For example, the zero value for an int is 0.
var e int
fmt.Println(e)
- := syntax - is shorthand for declaring and initializing a variable.
f := "20"
While declaring variable in Golang below naming convention should be followed -
- A variable or constant name can only start with a letter or an underscore.
- It can be followed by any number of letters, numbers or underscores after that
- Go is case sensitive so uppercase and lowercase letters are treated differently.
- The variable or constant name cannot be any keyword name in Go
- There is no limit on the length of variable or constant name. But is advisable to have the variable name of optimum length.
Previous Article
Next Article
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go