Reader small image

You're reading from  Go Programming - From Beginner to Professional - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781803243054
Edition2nd Edition
Languages
Right arrow
Author (1)
Samantha Coyle
Samantha Coyle
author image
Samantha Coyle

Samantha Coyle, a Software Engineer at Diagrid, specializes in Go for cloud-native developer tooling, abstracting application development challenges. Committed to Open Source, she contributes to projects like Dapr and Testcontainers. She boasts a rich history in retail computer vision solutions and successfully stabilized industrial edge use cases with testing and diverse deployments for biopharma data pipelines. Her expertise extends to being CKAD certified and reviewing Go textbooks. She is passionate about empowering early-career, diverse professionals. Samantha is in a family of gophers, and enjoys GopherCon with her brother and identical twin sister. She's a seasoned speaker, having presented at various conferences, including GopherCon.
Read more about Samantha Coyle

Right arrow

Changing the value of a variable

Now that we’ve defined our variables, let’s see what we can do with them. First, let’s change the value from its initial value. To do that, we’ll use a similar notation to when we set an initial value. This looks like <variable> = <value>.

Exercise 1.07 – changing the value of a variable

Follow these steps:

  1. Create a new folder and add a main.go file to it.
  2. In main.go, add the main package name to the top of the file:
    package main
  3. Import the packages we’ll need:
    import "fmt"
  4. Create the main() function:
    func main() {
  5. Declare a variable:
      offset := 5
  6. Print the variable to the console:
      fmt.Println(offset)
  7. Change the value of the variable:
      offset = 10
  8. Print it to the console again and close the main() function:
      fmt.Println(offset)
    }
  9. Save the file. Then, in the new folder, run the following:
    go run .

The following is the output before changing the variable’s value:

5
10

In this example, we’ve changed the value of offset from its initial value of 5 to 10. Anywhere you use a raw value, such as 5 and 10 in our example, you can use a variable. Here’s how that looks:

package main
import "fmt"
var defaultOffset = 10
func main() {
  offset := defaultOffset
  fmt.Println(offset)
  offset = offset + defaultOffset
  fmt.Println(offset)
}

The following is the output after changing the variable’s value:

10
20

Next, we’ll look at how we can change multiple variables in a one-line statement.

Changing multiple values at once

In the same way that you can declare multiple variables in one line, you can also change the value of more than one variable at a time. The syntax is similar, too; it looks like <var1>, <var2>, …, <varN> = <val1>, <val2>, …, <valN>.

Exercise 1.08 – changing multiple values at once

In this exercise, we’ll define some variables and use a one-line statement to change their values. Then, we’ll print their new values to the console. Let’s get started:

  1. Create a new folder and add a main.go file to it.
  2. In main.go, add the main package name to the top of the file:
    package main
  3. Import the packages we’ll need:
    import "fmt"
  4. Create the main() function:
    func main() {
  5. Declare our variables with an initial value:
      query, limit, offset := "bat", 10, 0
  6. Change each variable’s values using a one-line statement:
      query, limit, offset = "ball", offset, 20
  7. Print the values to the console and close the main() function:
      fmt.Println(query, limit, offset)
    }
  8. Save the file. Then, in the new folder, run the following:
    go run .

The following is the output showing the changed variable values using a single statement:

ball 0 20

In this exercise, we were able to change multiple variables in a single line. This approach would also work when calling functions, just as it does with a variable declaration. You need to be careful with a feature like this to ensure that, first and foremost, your code is easy to read and understand. If using a one-line statement like this makes it hard to know what the code is doing, then it’s better to take up more lines to write the code.

Next, we’ll look at what operators are and how they can be used to change your variables in interesting ways.

Previous PageNext Page
You have been reading a chapter from
Go Programming - From Beginner to Professional - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781803243054
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Samantha Coyle

Samantha Coyle, a Software Engineer at Diagrid, specializes in Go for cloud-native developer tooling, abstracting application development challenges. Committed to Open Source, she contributes to projects like Dapr and Testcontainers. She boasts a rich history in retail computer vision solutions and successfully stabilized industrial edge use cases with testing and diverse deployments for biopharma data pipelines. Her expertise extends to being CKAD certified and reviewing Go textbooks. She is passionate about empowering early-career, diverse professionals. Samantha is in a family of gophers, and enjoys GopherCon with her brother and identical twin sister. She's a seasoned speaker, having presented at various conferences, including GopherCon.
Read more about Samantha Coyle