Get to know variables and constants in Golang
Variables
The following is an example of the basic implementation of using variables in the program below
1package main
2
3import "fmt"
4
5func main() {
6 var x string
7 x = "Hello World"
8 fmt.Println(x)
9}In this program we will print the string Hello World by first saving the string into a variable namely x and then printing the variable using the fmt.Println() function.
If we want to combine string variables, we can use the + operation to combine them. For example, an example program is below:
1package main
2
3import "fmt"
4
5func main() {
6 var x string
7 x = "first"
8 fmt.Println(x)
9 x = x + "second"
10 fmt.Println(x)
11}After the program is run, the output is as below
1$ go run main.go
2first
3first secondWe can also use a different equality symbol ==. (Two equal signs next to each other) == is an operator like + and returns a boolean. As an example:
1package main
2
3import "fmt"
4
5func main() {
6 var x string = "hello"
7 var y string = "world"
8 fmt.Println(x == y)
9}This program will output false.
Meanwhile, if we change the string variable y to hello like this
1 ...
2 var y string = "world"
3 ...then the program will output true.
In Go we can also create variables more briefly and easily, for example:
1 x := "Hello World"The quotation mark : before the same as = indicates that we will store the contents in the variable x and the type of variable x will also be adjusted by the compiler, namely in the form of type string.
For example, let’s create another variable like the one below
1 x := 5
2 fmt.Println(x)x to be of type integerHow to define a variable
Naming variables correctly is an important part of software development. The name must start with a letter and can contain letters, numbers, or the _ (underscore) symbol. The Go compiler doesn’t care what you name the variables so the names are intended for your (and others’) benefit. Choose a name that clearly describes the variable’s purpose. Suppose we have the following:
1 x := "Kitty"
2 fmt.Println("Nama kucing saya adalah ", x)In this case x is not a very good name for a variable. A better name would be:
1 name := "Kitty"
2 fmt.Println("Nama kucing saya adalah ", name)or it could be
1 catName := "Kitty"
2 fmt.Println("Nama kucing peliharaanku adalah ", catName)Scope
Returning to the program, for example, we define variables as below
1package main
2
3import "fmt"
4
5func main() {
6 var x string
7 x = "Hello World"
8 fmt.Println(x)
9}then we want to edit to be like this
1package main
2
3import "fmt"
4
5var x string = "Hello World"
6
7func main() {
8 fmt.Println(x)
9}Note that we move the variable in out main function for the other function can access the variable like this
1package main
2
3import "fmt"
4
5var x string = "Hello World"
6
7func main() {
8 fmt.Println(x)
9}
10
11func f(){
12 fmt.Print(x)
13}f() will access x variable if the function called. then we can se this program like this: 1package main
2
3import "fmt"
4
5
6func main() {
7 var x string = "Hello World"
8 fmt.Println(x)
9}
10
11func f(){
12 fmt.Print(x)
13}1 .\main.go:11: undefined: xThe compiler tells us that the variable x in the function f() does not exist. It’s just inside the main function. The range of places where it is permissible to use x is called variable scope.
Basically, variables are inside the closest curly brace {} (block) including nested curly braces (block), but not outside them. Scopes can be a little confusing at first; as we see more Go examples, it will become clearer.
Constants
Go also has support for constants. Constants are basically variables whose values cannot be changed later. They are created in the same way as creating variables but instead of using the var keyword, we use the const keyword.
1package main
2
3import "fmt"
4
5func main() {
6 const x string = "Hello World"
7 fmt.Println(x)
8}Then we try adding 1 line below the constant like this
1 const x string = "Hello World"
2 x = "Some other string"then the compiler will issue an error like this
1.\main.go:7: cannot assign to xThe use of constant is only used for variables that will not be written every time. For example, Pi in mathematics can be used as a constant type.
Multiple Definitions
Go can also make it easier to initialize variables using multiple definitions as in the example below.
1var (
2 a = 5
3 b = 10
4 c = 15
5)This multiple definition can be used for var and const.
Program to read variables
The following is an example of inputting variables with a scan program
1package main
2
3import "fmt"
4
5func main() {
6 fmt.Print("Enter a number: ")
7 var input float64
8 fmt.Scanf("%f", &input)
9 output := input * 2
10 fmt.Println(output)
11}then output:
1Enter a number: 4
28