Answers for "How to read an input in golang"

-1

How to read an input in golang

package main

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

func main() {

    names := make([]string, 0)

    scanner := bufio.NewScanner(os.Stdin)
    
    for {
        fmt.Print("Enter name: ")
        
        scanner.Scan()
        
        text := scanner.Text()

        if len(text) != 0 {

            fmt.Println(text)
            names = append(names, text)
        } else {
            break
        }
    }

    fmt.Println(names)
}
Posted by: Guest on May-13-2021

Browse Popular Code Answers by Language