Reading User Input from Command Line in Go

Table of Contents

  1. Overview
  2. Prerequisites
  3. Setup
  4. Reading User Input
  5. Example: User Input in a Program
  6. Recap

Overview

In many programs, the ability to read user input from the command line is essential. Go provides various methods and packages to achieve this. In this tutorial, we will explore how to read user input using different approaches in Go.

By the end of this tutorial, you will be able to:

  • Understand the methods to read user input in Go
  • Use the bufio package to read user input
  • Implement user input reading in a Go program

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the Go programming language. Familiarity with basic syntax, variables, and data types will be helpful.

Setup

Before we begin, make sure you have Go installed on your system. You can download and install Go by following the official documentation found on the Go website.

Once Go is installed, create a new directory for this tutorial and navigate to it in your terminal or command prompt.

Reading User Input

There are several ways to read user input from the command line in Go. Here, we will discuss two common approaches:

  1. Using the fmt package

  2. Using the bufio package

Using the fmt Package

The fmt package in Go provides various functions to read user input. One of the most commonly used functions is fmt.Scanln(), which reads input until a newline character is encountered.

To read user input using the fmt package, follow these steps:

  1. Import the fmt package in your Go program:
     import "fmt"
    
  2. Declare a variable to store the user input.
     var input string
    
  3. Use the fmt.Scanln() function to read user input:
     fmt.Scanln(&input)
    
  4. The user input will be stored in the input variable and can be used in further processing.

    Here’s an example that reads a string input from the user:

     package main
        
     import "fmt"
        
     func main() {
         var name string
        
         fmt.Print("Enter your name: ")
         fmt.Scanln(&name)
        
         fmt.Println("Hello,", name)
     }
    

Using the bufio Package

Another approach to reading user input in Go is using the bufio package. This package provides a buffered input mechanism for efficient reading of input streams.

To read user input using the bufio package, follow these steps:

  1. Import the bufio and os packages in your Go program:
     import (
         "bufio"
         "os"
     )
    
  2. Create a bufio.NewReader() instance to read from os.Stdin (standard input):
     reader := bufio.NewReader(os.Stdin)
    
  3. Use the reader.ReadString() function to read user input:
     input, _ := reader.ReadString('\n')
    
  4. The user input will be stored in the input variable and can be used in further processing.

    Here’s an example that reads a string input from the user:

     package main
        
     import (
         "bufio"
         "fmt"
         "os"
     )
        
     func main() {
         reader := bufio.NewReader(os.Stdin)
        
         fmt.Print("Enter your name: ")
         name, _ := reader.ReadString('\n')
        
         fmt.Println("Hello,", name)
     }
    

Example: User Input in a Program

Let’s create a simple program that asks the user for their age and determines if they are eligible to vote.

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func main() {
    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter your age: ")
    ageInput, _ := reader.ReadString('\n')

    age, _ := strconv.Atoi(ageInput)

    if age >= 18 {
        fmt.Println("You are eligible to vote!")
    } else {
        fmt.Println("You are not eligible to vote yet.")
    }
}

In the above example, we read the input using the bufio package and convert it to an integer using the strconv package’s Atoi() function. We then check if the user’s age is greater than or equal to 18 and display the appropriate message.

Recap

In this tutorial, we explored two different approaches to read user input from the command line in Go. We learned how to use the fmt package and the bufio package to achieve this.

Here are the key takeaways from this tutorial:

  • The fmt package provides functions like fmt.Scanln() to read user input.
  • The bufio package offers a buffered input mechanism for efficient reading of input streams.
  • Remember to import the necessary packages (fmt, bufio, os, etc.) before using them in your Go program.
  • Use the appropriate functions (Scanln() or ReadString()) to read user input based on the chosen approach.

You are now equipped with the knowledge to read user input from the command line in Go. Feel free to experiment with different input types and create more interactive programs using this concept.

Happy coding!