Golang 输入
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
// input a int num
var a int
fmt.Scan(&a)
fmt.Println(a)
var b, c int
fmt.Scan(&b, &c)
fmt.Println(b, c)
// input slice int string
// init a reader
reader := bufio.NewReader(os.Stdin)
//
res, _ := reader.ReadSlice('/n')
fmt.Println(res)
fmt.Println(string(res))
// how to input int slice
res, _ := reader.ReadString('/n')
fmt.Println(res)
fmt.Printf("%T", res)
arrs := intnums(res)
fmt.Println(arrs)
fmt.Printf("%T", arrs)
// how to input string slice
res, _ := reader.ReadString('/n')
fmt.Println(res)
fmt.Printf("%T", res)
arrs := sarr(res)
fmt.Println(arrs)
fmt.Printf("%T", arrs)
}
func intnums(s string) (res []int) {
for _, ss := range strings.Fields(s) {
num, _ := strconv.Atoi(ss)
res = append(res, num)
}
return
}
func sarr(s string) (res []string) {
for _, ss := range strings.Fields(s) {
res = append(res, ss)
}
return
}
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/274227.html