其他分享
首页 > 其他分享> > Golang 输入输出详解

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
}

标签:string,int,res,fmt,输入输出,Golang,详解,arrs,Println
来源: https://www.cnblogs.com/notomatoes/p/16267030.html