Working with variables and conditionals in Go Lang

Jonathan Fielding
3 min readJan 2, 2021

I recently started to learn Go Lang, and as part of this I have been learning about variables and how they differ to variables in other languages I have worked with such as JavaScript and PHP.

Getting to grips with variables

In Go, variables are explicitly declared and when you compile your application, Go will infer the type of the initialized variables and use this to check the type-correctness of your code.

To define a variable in Go Lang we use var followed by the name of the variable, equals, and the value we want to assign to the variable. We can update our ‘Hello World’ application from earlier to use variables for each of the words.

The first step is to create a new file, in this case, I used using-variables.go. We can then take the original code from our hello world application and create 2 variables, one for each of the words. We then update our fmt.Println to instead join the strings, adding a space in between.

--

--

Jonathan Fielding
Jonathan Fielding

Written by Jonathan Fielding

Staff Engineer working for @Spendesk, speaker about web things, writing about tech, contributor to open source. If you like what I write make sure to follow.

No responses yet