programming

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

package main

import "fmt"

func main() {
	var x string
	x = "Hello World"
	fmt.Println(x)
}

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:

package main

import "fmt"

func main() {
	var x string
	x = "first"
	fmt.Println(x)
	x = x + "second"
	fmt.Println(x)
}

After the program is run, the output is as below

$ go run main.go 
first 
first second

We 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:

package main

import "fmt"

func main() {
  var x string = "hello"
  var y string = "world"
  fmt.Println(x == y)
}

This program will output false. Meanwhile, if we change the string variable y to hello like this

  ...
  var y string = "world"
  ...

then the program will output true.

In Go we can also create variables more briefly and easily, for example:

  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

  x := 5
  fmt.Println(x)

So the compiler will consider the variable x to be of type integer

How 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:

  x := "Kitty"
  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:

  name := "Kitty"
  fmt.Println("Nama kucing saya adalah ", name)

or it could be

  catName := "Kitty"
  fmt.Println("Nama kucing peliharaanku adalah ", catName)

Scope

Returning to the program, for example, we define variables as below

package main

import "fmt"

func main() {
  var x string
  x = "Hello World"
	fmt.Println(x)
}

then we want to edit to be like this

package main

import "fmt"

var x string = "Hello World"

func main() {
	fmt.Println(x)
}

Note that we move the variable in out main function for the other function can access the variable like this

package main

import "fmt"

var x string = "Hello World"

func main() {
	fmt.Println(x)
}

func f(){
	fmt.Print(x)
}

Now the function f() will access x variable if the function called. then we can se this program like this:

package main

import "fmt"


func main() {
  var x string = "Hello World"
	fmt.Println(x)
}

func f(){
	fmt.Print(x)
}

If you will running this program that you found the error like this.

  .\main.go:11: undefined: x

The 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.

package main

import "fmt"

func main() {
	const x string = "Hello World"
	fmt.Println(x)
}

Then we try adding 1 line below the constant like this

  const x string = "Hello World"
  x = "Some other string"

then the compiler will issue an error like this

.\main.go:7: cannot assign to x

The 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.

var ( 
  a = 5
  b = 10
  c = 15 
)

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

package main

import "fmt"

func main() {
	fmt.Print("Enter a number: ")
	var input float64
	fmt.Scanf("%f", &input)
	output := input * 2
	fmt.Println(output)
}

then output:

Enter a number: 4
8
comments powered by Disqus