goinputstructgenerate

Generating new instance of struct from scanner input


I was attempting to run a program that created a new instance of a predefined struct from the users input.
The struct is comprised of 3 fields type string. The goal being for the user to input strings into a scanner, creating a new, unique instance of the struct, using the input as it's values. The program would be in a loop, allowing the user to create multiple instances of the struct.
In the code presented, there are 2 struct types; Boat and Car. The first string input would indicate which type of struct is chosen to be created, and the next 3 strings would be to fill the struct values.

package main

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

type Boat struct {
   name string
   color string
   design string
}
type Car struct {
   name string
   color string
   design string
}

func main() {

    TempBoat := Boat{"gerrard","red","speed"}
    TempCar := Car{"conroy","blue","cruiser"}

        /* I was using a template that would then be filled by the user, but 
          this only allows for one instance that would continue to be 
          overwritten. */

    scanner := bufio.NewScanner(os.Stdin)
    for {
        if scanner.Scan() {
            userIn := scanner.Text()
            scanMain := strings.Fields(userIn)
            Scan0 := scanMain[0]
            Scan1 := scanMain[1]
            Scan2 := scanMain[2]
            Scan3 := scanMain[3]
            if Scan0 == "car" {
                TempCar.name = Scan1
                TempCar.color = Scan2
                TempCar.design = Scan3
            } else if Scan0 == "boat" {
                TempBoat.name = Scan1
                TempBoat.name = Scan2
                TempBoat.name = Scan3
            } else {
                fmt.Println("Invalid Input. Try Again.")
            }
        }
    }
}

Solution

  • You didn't really specify what you wanted to do with the values being read so i just put them into a slice for safe keeping.

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "strings"
    )
    
    type Boat struct {
        name   string
        color  string
        design string
    }
    type Car struct {
        name   string
        color  string
        design string
    }
    
    func main() {
    
        var Boats = []Boat{}
        var Cars = []Car{}
    
        scanner := bufio.NewScanner(os.Stdin)
        for scanner.Scan() {
            userIn := scanner.Text()
            scanMain := strings.Fields(userIn)
            if len(scanMain) == 0 {
                break
            }
            if len(scanMain) != 4 {
                fmt.Println("Invalid Input. Try Again.")
            }
            Scan0 := scanMain[0]
            Scan1 := scanMain[1]
            Scan2 := scanMain[2]
            Scan3 := scanMain[3]
            if Scan0 == "car" {
                Cars = append(Cars, Car{Scan1, Scan2, Scan3})
            } else if Scan0 == "boat" {
                Boats = append(Boats, Boat{Scan1, Scan2, Scan3})
            } else {
                fmt.Println("Invalid Input. Try Again.")
            }
        }
        fmt.Println("Boats:", Boats)
        fmt.Println("Cars:", Cars)
    }