Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

if else statement

  • if
  • else

We'll have a longer discussion about conditinal statements later, but it is quite hard to do anything useful without them so let's have a quick look at an if - else construct. Here we check if the value in the name variable equals to the string Go and depending on the success of this comparision we print something.

package main

import (
	"fmt"
)

func main() {
	var name string
	fmt.Print("What is the name of this langauage? ")
	fmt.Scan(&name)

	if name == "Go" {
		fmt.Println("Yes, that's the answer!")
	} else {
		fmt.Println("Well..")
	}
}