# Go: Condition (Part 4)

This is a forth post on Go programming language. You can read the first, second and third posts if you are reading this post for the first time.

Go supports conditional statements via if, its partner else and supportive sheriff else if. As usual, the condition body will only going to execute if condition mentioned become true. For example, in below happy.go program, it print I'm happy man! because the condition is true.

package main

import "fmt"

func main() {
if true {
fmt.Println("I'm happy man!")
}
}


Pay attention to the syntax - No need to wrap the condition within parenthesis. Also, the opening curly brace for the condition must be on the same line. If you write it on the next line, it will generate an error. For example,

package main

import "fmt"

func main() {
if true
{
fmt.Println("I'm happy man!")
}
}


Running this program will generate the following error.

$go run happy.go ./prog.go:6:9: syntax error: unexpected newline, expecting { after if clause  Also, you must need to wrap the body of conditional statements inside the curly braces even the body contains only one statement. If you do not wrap the body using curly braces then it will generate an error. For example, package main import "fmt" func main() { if true fmt.Println("I'm happy man!") }  Running the above program will general following error. $ go run happy.go
./prog.go:7:39: syntax error: unexpected newline, expecting { after if clause


With the knowledge of conditional statement, you can write some useful programs. For example, let's write a program that check if the number is positive, negative or zero. You know the logic - just need to compare the number against zero. For example, I have written the num_check.go program as follow for this.

package main

import "fmt"

func main() {
var num int = 23

if num > 0 {
fmt.Println(num, "is positive.")
} else if num < 0 {
fmt.Println(num, "is negative.")
} else {
fmt.Println("Its zero.")
}
}


Running this program will produce following output.

$go run num-check.go 23 is positive  Using condition, we can also check if the given number is odd or even. Again you know the logic right - just check the reminder of division of given number by 2. If the reminder is 0 then the given number is even else the given number is odd. The reminder of division can be obtain via % or modulo operator. So, 7 % 2 produces 1 and 16 % 2 produces 0. Here is the program odd_even.go that check if the given number is odd or even. package main import "fmt" func main() { var num int = 23 if num % 2 == 0 { fmt.Println(num, "is even.") } else { fmt.Println(num, "is odd.") } }  If I run this program, it will generate following output. $ go run odd-even.go
23 is odd.


You know, in order to check both statement, I have to go inside of the given program and explicitly need to change the value of num. We can eliminate the situation by taking the value from user using Scanf() function.

This Scanf() function is part of fmt package so you need to use it like -

fmt.Scanf()


This function take two arguments - formate specifier and the location of variable where we want to save the user entered value. The format specifier helps with the type of value that it will accept. If you write %d, it will accept integers. If you write %f, it will accept floating point numbers. If you write %s, it will accept string. If you write %t, it will accept boolean values. For our odd_even.go program, num is integer so we will write -

fmt.Scanf("%d")


The second argument is location or the address of variable. You can define the address of variable using & operator known as address of operator. If you want to define the address of num variable, just write &num like,

fmt.Scanf("%d", &num)


That's it! This will accept the num value from user and save it at the address of num variable so that it can be access then for further modification. Now, if I add this line in odd_even.go program, it will become,

package main

import "fmt"

func main() {
var num int = 23

fmt.Scanf("%d", &num)

if num % 2 == 0 {
fmt.Println(num, "is even.")
} else {
fmt.Println(num, "is odd.")
}
}


Let's run this program.

$go run odd_even.go 23 23 is odd.  You might notice that I don't know when to enter the number because it was compiling. It is better to write some text before this so that we know what we are entering like this. package main import "fmt" func main() { var num int = 23 fmt.Print("Enter the value of number: ") fmt.Scanf("%d", &num) if num % 2 == 0 { fmt.Println(num, "is even.") } else { fmt.Println(num, "is odd.") } }  With this modification, let's run this program. $ go run odd_even.go
Enter the value of number: 23
23 is odd.


Now it is better! Any questions so far?

Why I can not write opening curly brace in next line? Remember the one-line-per-statement rule? Go compiler injects a semicolon after each line ends in normal scenario. When you write an opening curly brace in new line, Go compiler thoughts a statement ends with newline and it inject semicolon after condition in if statement. Hence, it cause the syntax break.

What if I entered name instead of number in last program? Nothing and it will print 23 is odd. Because, the value is already defined. But, if you pass any string such as Bruce, it will not accept it and just go with default value.

I Want to take more than one values from user, how can I do it? Just write the bunch of Scanf() functions. For example, I want to calculate the simple interest from the values given by user. Here is how you can possibly do it.

package main

import "fmt"

func main() {
var p, r, n, si float64

fmt.Print("Enter the value of p: ")
fmt.Scanf("%f", &p)

fmt.Print("Enter the value of r: ")
fmt.Scanf("%f", &r)

fmt.Print("Enter the value of n: ")
fmt.Scanf("%f", &n)

si = p * r * n / 100

fmt.Println("The simple interest is", si)
}


"the opening curly brace for the condition must be on the same line"

It's interesting to see a compiled language having an opinion on this. Especially one that differs from C syntax, which Go's syntax is supposedly similar to. Is there a reason for this style preference and are there other similar stylistic requirements?

This series is starting to get interesting now that it's moving beyond the basic constructs (variables, operators, control flow) that are shared with other languages.

It would be interesting to learn about some of the aspects that make Go unique, such as package structure, compiler & static analysis, testing framework and approach to concurrency with goroutines and channels. Also, potentially to get some idea of what a typical Go project looks like.

Great job so far Kiran, really enjoying this. Thanks for sharing it.

Is there a reason for this style preference

He answered that in the first QA question:

Remember the one-line-per-statement rule? Go compiler injects a semicolon after each line ends in normal scenario. When you write an opening curly brace in new line, Go compiler thoughts a statement ends with newline and it inject semicolon after condition in if statement. Hence, it cause the syntax break.