To create a new variable in Go we use thevar
keyword, followed by the variable name (x
) and type (string
) and then assign it a value (“Hello”
).
var x string = “Hello”
Assigning value is optional, so we could also write it
var x string
x = “Hello”
Variables are mutable. This means they can be changed. So we can change x from“Hello”
to something else:
x = "Hi"
When creating a new variable with a starting value, Go can infer the type, so we can do this:
var age = 5
Generally you should use the shorter syntax for this whenever possible:
age := 5
Constants are a special type of variable that are immutable. This means it cannot be reassigned. They are created the same way as variables except you use theconst
keyword instead of var
. Again, Go can infer the type from the assigned value.
const x = “Hello”
// The following line would create an error
x = "Hi"
You can also assign multiple variables or constants (using const
instead of var
) at once:
var(
a = 5
b = 10
c = 15
)
Self taught software developer with 11 years experience excelling at JavaScript/Typescript, React, Node and AWS.
I love learning and teaching and have mentored several junior developers over my career. I find teaching is one of the best ways to solidify your own learning, so in the past few years I've been maintaining a technical blog where I write about some things that I've been learning.
I'm passionate about building a teams culture and processes to make it efficient and satisfying to work in. In many roles I have improved the quality and reliability of the code base by introducing or improving the continuous integration pipeline to include quality gates.