Basics of Go: Variables and it's Magic
| Reading Time : 1 minute | #backenddevelopment #Go
[TOC]
There are many ways to assign a variable in Go. Here below are some of those which I have encountered till date.
Syntax: var i int
package main
import "fmt"
func main() {
var i int
fmt.Println(i)
}
The var
statement can be at either package or function level and it declares a list of variables the type is last.
If an initializer is present, the type can be omitted and the variable will assume the type of the initializer. Example :
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!"
fmt.Println(i, j, c, python, java)
}
Interesting syntax :=
Inside a function, the :=
short assignment statement can be used in place of a var
declaration with implicit type.
Example:
package main
import "fmt"
func main() {
var k int = 3
//:= is short assignment can be used in place of var declaration with implicit type.
l := 3
fmt.Println(k, l)
}
Output :
k is 3
l is 3
powerctl is a little weekend project I put together to provide a simple tool for managing power states on Linux. I had previously put my laptop into suspend with a basic “echo mem | doas tee /sys/power/state”, but this leaves a lot to be desired. I have to u…
via Drew DeVault's blog August 28, 2022Links to exhibits from the Microsoft anti-trust case, with a bit of info on each link. Projection of PC marketshare Share of new browser users Share of the browser market, grouped by major ISP, 3 month moving average Share of the browser market, grouped by ma…
via danluu.com August 24, 2022Hi all! This month I’ve been pondering offline-first apps. The online aspect of modern apps is an important feature for many use-cases: it enables collaboration between multiple people and seamless transition between devices (e.g. I often switch between my pe…
via emersion August 14, 2022Generated by openring