Getting started with Go: Part 2 – Variables and Constants

In the previous post we learned how to install go and we printed hello world as a test. For this post, we’re gonna take it a step further by setting up a project and playing with some variables and functions. Let’s start by firing up our text editors and creating a folder in our home directory called go-projects. Inside go-projects, we’re gonna create another folder called variables.

Variables

Variables in Go are like little boxes that hold information for any reason. Let’s use our hello world example from the previous tutorial and take it a few steps further. We’ll start by creating a new file in our variables folder called main.go and paste in the code from the last tutorial.

package main

import “fmt”

func main() {
    fmt.Printf(“Hello, world!\n”)
}

Now let’s introduce our first local variable within the main function.

package main

import “fmt”

func main() {
    var planet string = “jupiter”
    
    fmt.Printf(“Hello, %s!\n”, planet)
}

Here, we created a variable named planet that is of type string and printed it to the console using a formatted string (more on those later). Sometimes, you will be in a situation where you want to declare an empty variable and assign a value to it later. In that case, you would do something like this:

var planet string
...
...
...
planet = “jupiter”

Multiple empty variables can be declared in a single line as long as they’re of the same type.

var mars, jupiter, saturn string

Go also allows you to declare variables without using the var keyword.

package main

import “fmt”

func main() {
    planet := “jupiter”
    
    fmt.Printf(“Hello, %s!\n”, planet)
}

Running go run main.go on either of these examples should get you the results:

Hello jupiter!

This tends to be the more common way to declare variables since programmers (especially me) are lazy and would like to type as less as possible.

In addition to strings, Go allows for multiple ways to define numbers like int8, int32, int64 among others. The numbers on the ends of these types refer to how many bytes of computer memory they can hold. So int64 can hold bigger numbers than int8 because it has more memory. Here’s a quick example of using integers to find out how many weeks are in a year.

We’ll create two int (short for integer) variables; one that will hold the number of days in a week and another that will hold the number of days in a year. Finally, we’ll print out the results of dividing the days in a year by the days in a week

package main

import "fmt"

func main() {

  daysInAWeek := 7
  daysInAYear := 365
  weeksInAYear := daysInAYear / daysInAWeek
  fmt.Printf(“weeks in a year: %d\n”, weeksInAYear)
}

When running go run main.go here, you get the following:

weeks in a year: 52

As you can see, mathematical operations can be done with these int variables. For that example we went with division. Similar to adding two ints, string concatenation can also be done in Go, here’s a quick example:

package main

import "fmt"

func main() {

  daysInAWeek := 7
  daysInAYear := 365
  weeksInAYear := daysInAYear / daysInAWeek
  fmt.Printf("weeks in a year: %d\n", weeksInAYear)

  pt1 := "weeks in"
  pt2 := "a year"
  weeksInAYearString := pt1 + " " + pt2
  fmt.Printf("%s: %d\n", weeksInAYearString, weeksInAYear)
}

So now, in addition to what we had earlier, we made two more strings that combine to make the same string. We also used the weeksInAYear int variable again just for consistency. In our second formatted string we set two placeholders, one string (%s) and one integer (%b), and pass in our new cool string to show off concatenation. When running this we get:

weeks in a year: 52
weeks in a year: 52

Constants

Similar to variables, Go allows one to use constants which are basically values that stay the same. Constants are typically strings or numbers, the months on the US calendar would be a good example here. Constands can defined two ways:

const CONSTNAME = “Value that doesnt change”

And if you have multiple…

const (
    CONSTINT1 = 1
    CONSTINT2 = 2
    CONSTINT3 = 3
)

Let’s add some month constants to our variables.go file and print them out.

package main

import "fmt"

const (
  JANUARY = "Jan"
  FEBRUARY = "Feb"
  MARCH = "Mar"
)


func main() {

  daysInAWeek := 7
  daysInAYear := 365
  weeksInAYear := daysInAYear / daysInAWeek
  fmt.Printf("weeks in a year: %d\n", weeksInAYear)

  pt1 := "weeks in"
  pt2 := "a year"
  weeksInAYearString := pt1 + " " + pt2
  fmt.Printf("%s: %d\n", weeksInAYearString, weeksInAYear)
  fmt.Println(JANUARY, FEBRUARY, MARCH)

}

We should get back the following:

weeks in a year: 52
weeks in a year: 52
Jan Feb Mar

One last cool thing that works with storing integer constants is Go’s iota identifier. This is a handy way to match integer values to constants without having to manually write out each number. Go’s identifier iota starts with a value of 0 so a good way to use it for months in a year would be like this:

const (
  JANUARY = iota + 1
  FEBRUARY
  MARCH
)

If we swap these in with the strings we had before and run everything again we’d get this, now.

weeks in a year: 52
weeks in a year: 52
1 2 3

That’s all for this tutorial. Today we learned about how to declare variables and constants in Go and printing them using Go’s fmt library. Let me know if there is anything else you would like to see. Any questions, comments, or any other feedback would be greatly appreciated. Thanks for reading!

Leave a Reply

Close Menu
×
×

Cart