Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Modifying Variable Values | Getting Started
Introduction to GoLang

bookModifying Variable Values

We've learned how to declare a variable. Now, let's understand how to modify the value of a variable.

Take a look at the following program, which prints the value of myVariable using the Println function:

index.go

index.go

copy
1234567
package main import "fmt" func main() { var myVariable = 7 fmt.Println(myVariable) // Using the name of the variable ('myVariable') we can access and show it's value }

We can use the following syntax for assigning a different value to the variable:

index.go

index.go

copy
1
myVariable = 9

Notice that this time, we omit the var keyword because we are not declaring a new variable. Now, let's add this line to the program:

index.go

index.go

copy
123456789
package main import "fmt" func main() { var myVariable = 7 fmt.Println(myVariable) // Outputs 7 myVariable = 9 fmt.Println(myVariable) // Outputs 9 }

In the code above, we employed the var keyword to declare a variable. Alternatively, we can use the := operator for variable declaration. In such a case, the code will appear as follows:

index.go

index.go

copy
123456789
package main import "fmt" func main() { myVariable := 7 fmt.Println(myVariable) // Outputs 7 myVariable = 9 fmt.Println(myVariable) // Outputs 9 }
question mark

Which is the correct way of changing the value of an already declared variable to 10 called myVar:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 1.96

bookModifying Variable Values

Свайпніть щоб показати меню

We've learned how to declare a variable. Now, let's understand how to modify the value of a variable.

Take a look at the following program, which prints the value of myVariable using the Println function:

index.go

index.go

copy
1234567
package main import "fmt" func main() { var myVariable = 7 fmt.Println(myVariable) // Using the name of the variable ('myVariable') we can access and show it's value }

We can use the following syntax for assigning a different value to the variable:

index.go

index.go

copy
1
myVariable = 9

Notice that this time, we omit the var keyword because we are not declaring a new variable. Now, let's add this line to the program:

index.go

index.go

copy
123456789
package main import "fmt" func main() { var myVariable = 7 fmt.Println(myVariable) // Outputs 7 myVariable = 9 fmt.Println(myVariable) // Outputs 9 }

In the code above, we employed the var keyword to declare a variable. Alternatively, we can use the := operator for variable declaration. In such a case, the code will appear as follows:

index.go

index.go

copy
123456789
package main import "fmt" func main() { myVariable := 7 fmt.Println(myVariable) // Outputs 7 myVariable = 9 fmt.Println(myVariable) // Outputs 9 }
question mark

Which is the correct way of changing the value of an already declared variable to 10 called myVar:

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
some-alt