Golang 输入输出详解
2022/7/14 6:20:10
本文主要是介绍Golang 输入输出详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 }
这篇关于Golang 输入输出详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-04-01got an unexpected keyword argument
- 2024-03-30维多利亚的秘密 golang入坑系统
- 2024-03-29mongodb sort by date
- 2024-03-29go swagger
- 2024-03-25mongodb cdc
- 2024-03-25how to use go in vscode
- 2024-03-22mongooseserverselectionerror: connect econnrefused ::1:27017
- 2024-03-21pymongo insert_many
- 2024-03-18projection mongodb
- 2024-03-14clickhouse-go