DEV Community

Cover image for Working with variables and conditionals in Go Lang
Jonathan Fielding
Jonathan Fielding

Posted on • Originally published at jonthanfielding.Medium

Working with variables and conditionals in Go Lang

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.

package main

import "fmt"

func main() {
    var hello = "hello"
    var world = "world"

    fmt.Println(hello + " " + world)
}
Enter fullscreen mode Exit fullscreen mode

We can then run this example:

go run 02-using-variables.go
Enter fullscreen mode Exit fullscreen mode

This will result in the words hello world being logged to the console. While this is a fairly contrived example, is shows us how we can easily update code which previously hard coded to now be customisable.

Introducing conditional logic to our application

The next thing we are going to learn about is how we can introduce conditional logic to our hello world application. In this case I want to allow the user to have the option to provide a name and instead of saying hello to the world we will say hello to that individual person instead 😀.

The first step is to create our new file for the new version of the application. I have called my file conditional-logic.go and as a starting point I copied and pasted the code from our previous application.

We now need to allow our application to get user input. To do this we need to import the os package which will make the arguments available to us as an Array. To access this Array we use os.Args, the first item of which will be the path to our application and the rest of the values will be the arguments we passed to it.

We only want to set the name when an argument has been passed, to do this we can use an if statement. In this if statement we want to check that the length of the arguments, excluding the application path is equal to one, we do this with the following.

if len(os.Args[1:]) == 1 {
    name = os.Args[1]
}
Enter fullscreen mode Exit fullscreen mode

If we incorporate this into our completed code it will look as follows:


package main

import (
  "fmt"
  "os"
)

func main() {
  var hello = "hello"
  var name = "world"

  if len(os.Args[1:]) == 1 {
    name = os.Args[1]
  }

  fmt.Println(hello + " " + name)
}
Enter fullscreen mode Exit fullscreen mode

To run our application we can simply run the command:

go run conditional-logic.go
Enter fullscreen mode Exit fullscreen mode

This will still output hello world however if we now pass a name as an additional argument:

go run conditional-logic.go jonathan
Enter fullscreen mode Exit fullscreen mode

The output will now be hello jonathan, this is exactly what we wanted to achieve 🤗.

Wrapping up

Thank you for taking the time to read about using variables in Go Lang, it was good to start playing with variables and conditionals and I can definitely see similarities to how I would write the same thing in JavaScript.

Top comments (1)

Collapse
 
pankajpatel profile image
Pankaj Patel

Thanks for whining about it!

I am also learning go and had gone through essentials course on LinkedIn Learning.

For variables I remember one syntax as

var name := “dev.to”

I don’t remember exactly how it was. It would be great addition to your article; and a confusion removal for me ;)