20240430

fmt.Scan()

ref. I thought fmt.Scan() would work fine for me, but when I think about scanning multiple values from stdin, I need a scanner to process the input string by token. (fmt.Scan() and fmt.Scanln() don't support reading the entire line.)

For example,

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanLines)

    var input string
    scanner.Scan()
    input = scanner.Text()

    var values []int

    for _, text := range strings.Fields(input) {
        num, err := strconv.Atoi(text)
        if err != nil {
            fmt.Println("Error converting input:", err)
            return
        }
        values = append(values, num)
    }

    fmt.Println("Scanned values:", values)
}

I don't feel this is the optimal way to achieve that tho. will check some code online


Kombucha 100 Protein chips 150 Yogurt 250

Avocado Rolls 300 Poke 600 Chips 300

Total 1700kcal


TODO:


index 20240429 20240501