User Input in Go language

Yasir ArafatYasir Arafat
2 min read

In order to take user input in Go language we need to create a reader variable using some inbuilt modules of Go

package main

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

func main() {
    fmt.Println("hi there")
    reader := bufio.NewReader(os.Stdin)

    fmt.Println("Enter your name: ")
    name, _ := reader.ReadString('\n')

    fmt.Println("you entered ", name)
}

bufio.Reader

The bufio.Reader is a type in Go's bufio package that provides buffered I/O operations for reading data from an underlying io.Reader. It helps to efficiently read data from sources like files, network connections, or standard input.

Code Explanation

goCopy codereader := bufio.NewReader(os.Stdin)

In this line, we create a new bufio.Reader instance by passing os.Stdin as the underlying reader. os.Stdin represents the standard input stream, which allows the program to read input from the user's keyboard.

goCopy codename, _ := reader.ReadString('\\n')

The reader.ReadString('\\n') function reads input from the underlying reader until the delimiter '\\n' (newline character) is encountered. It returns the data read as a string, including the delimiter.

The second value (_ in this case) is an error value. If there's no error during the read operation, it will be nil. However, in this code, the error value is ignored using the blank identifier _.

Delimiter

In the context of reader.ReadString('\\n'), the delimiter '\\n' represents the newline character. The function reads input until it encounters this character, which typically signifies the end of a line of user input.

Comma OK or Comma Error Syntax

The syntax name, _ := reader.ReadString('\\n') is an example of the "comma ok" syntax in Go. It's a way to handle multiple return values from a function.

  • name is the first return value, which is the string read from the input.

  • _ is the second return value, which represents the error value. By assigning it to the blank identifier _, we're explicitly ignoring the error value.

This syntax is often used when you're interested in the first return value but don't want to handle the error value immediately. However, it's generally recommended to handle errors appropriately in production code.

Reader Functions

The bufio.Reader provides several functions for reading data. Here are some common ones:

  1. ReadString(delim byte): Reads data until the provided delimiter byte is encountered.

  2. ReadBytes(delim byte): Similar to ReadString, but returns a []byte instead of a string.

  3. ReadLine(): Reads a single line of data, excluding the newline character.

  4. Read(p []byte): Reads data into the provided byte slice p until the buffer is full or an error occurs.

  5. ReadByte(): Reads and returns the next byte from the buffer.

  6. ReadSlice(delim byte): Reads data until the provided delimiter byte is encountered, returning a slice of bytes.

0
Subscribe to my newsletter

Read articles from Yasir Arafat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yasir Arafat
Yasir Arafat

๐Ÿ‘‹ Hello there! I'm Yasir Arafat, a motivated 3rd-year undergraduate at IIIT-Allahabad, driven by a passion for Data Structures and Algorithms (DSA) and Web Development. ๐ŸŒŸ Achievements: Currently holding the esteemed title of a Knight at Leetcode and Specialist at Codeforces. Consistently achieving ranks near 1800 in various DSA/CC contests. ๐Ÿ’ป Technical Expertise: In-depth understanding of the MERN stack, with a focus on backend development. Noteworthy projects include the creation of a Virtual Trading Platform and a Student Portfolio Management system. Knowledgeable in Object-Oriented Programming (OOP), Database Management Systems (DBMS), and Operating Systems (OS). Proficient in Git version control. ๐Ÿš€ Outlook: Excited about the convergence of technology and creativity, I am open to new opportunities and collaborations.