Go Lesson 9 –Input & Output | Dataplexa

Input and Output in Go

Input and output are essential in every program. Input allows users or systems to provide data, while output displays results.

In Go, input and output are commonly handled using the fmt package. This package provides simple and powerful functions for reading and printing data.


Printing Output Using fmt.Println

The most common way to display output in Go is using fmt.Println. It prints values followed by a new line.

package main

import "fmt"

func main() {
    fmt.Println("Welcome to Go Programming")
    fmt.Println(100)
    fmt.Println(25.5)
}

Each value is printed on a new line automatically.


Using fmt.Print

fmt.Print prints output without moving to a new line.

package main

import "fmt"

func main() {
    fmt.Print("Hello ")
    fmt.Print("Go")
}

This is useful when you want full control over formatting.


Formatted Output Using fmt.Printf

fmt.Printf allows formatted output using format specifiers. This is commonly used in reports, logs, and real applications.

package main

import "fmt"

func main() {
    name := "Alex"
    age := 28
    salary := 75000.50

    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Age: %d\n", age)
    fmt.Printf("Salary: %.2f\n", salary)
}

Common format specifiers:

  • %s → string
  • %d → integer
  • %f → floating-point number

Taking User Input Using fmt.Scan

Go allows you to read user input from the keyboard using fmt.Scan.

This is commonly used in command-line tools and interactive programs.

package main

import "fmt"

func main() {
    var name string
    var age int

    fmt.Print("Enter your name: ")
    fmt.Scan(&name)

    fmt.Print("Enter your age: ")
    fmt.Scan(&age)

    fmt.Println("Name:", name)
    fmt.Println("Age:", age)
}

The & symbol is used to store the input value into the variable.


Reading Multiple Inputs

You can read multiple values at once using a single fmt.Scan call.

package main

import "fmt"

func main() {
    var x, y int

    fmt.Print("Enter two numbers: ")
    fmt.Scan(&x, &y)

    fmt.Println("Sum:", x+y)
}

This is useful for mathematical and data-processing applications.


Using fmt.Scanln

fmt.Scanln works like Scan but stops reading at a new line.

package main

import "fmt"

func main() {
    var city string

    fmt.Print("Enter your city: ")
    fmt.Scanln(&city)

    fmt.Println("City:", city)
}

Using fmt.Scanf

fmt.Scanf allows formatted input similar to Printf.

package main

import "fmt"

func main() {
    var product string
    var price float64

    fmt.Scanf("%s %f", &product, &price)

    fmt.Printf("Product: %s\nPrice: %.2f\n", product, price)
}

This is useful when input follows a fixed format.


Real-World Example

Let’s build a simple billing program using input and output.

package main

import "fmt"

func main() {
    var quantity int
    var price float64

    fmt.Print("Enter quantity: ")
    fmt.Scan(&quantity)

    fmt.Print("Enter price per item: ")
    fmt.Scan(&price)

    total := float64(quantity) * price

    fmt.Println("Total bill amount:", total)
}

This demonstrates how Go handles real user input and calculations.


Common Input Errors

If input type does not match the variable type, Go will not assign the value.

Always ensure correct data types and input order.


Why Input and Output Are Important

  • They make programs interactive
  • They allow communication with users
  • They are essential for real-world applications

What’s Next?

In the next lesson, you will learn how to convert data types in Go using type conversion techniques.