// 命令行参数 func test() { var b bool var path string var port int // 预处理配置 flag.BoolVar(&b, "b", false, "print on newline") flag.StringVar(&path, "c", "/conf.config", "Specify the configuration file. The default is /conf.config .") flag.IntVar(&port, "p", 8080, "Specify the port, the default is 8080") // 解析命令行标志 flag.Parse() fmt.Printf("bool is %v\n", b) fmt.Printf("path is %s\n", path) fmt.Printf("port is %d\n", port) }
func main() { // 带缓冲区的读 var inputReader *bufio.Reader inputReader = bufio.NewReader(os.Stdin) fmt.Println("please enter some input:") input, err := inputReader.ReadString('\n') if err != nil { fmt.Printf("input err:\n", err) } fmt.Printf("the input was %s\n", input) } ------------------------ please enter some input: 123 the input was 123
type charCount struct { c int n int spance int other int }
func test() { file, err := os.Open("/1.txt") if err != nil { fmt.Printf("open file err:%s\n", err) return } defer file.Close() var count charCount var inputReader *bufio.Reader inputReader = bufio.NewReader(file) for { str, err1 := inputReader.ReadString('\n') if err1 == io.EOF { fmt.Printf("read to the end of file\n") break } if err != nil { fmt.Printf("read file err:%s", err) break }
ru := []rune(str) for _, v := range ru { switch { case v >= 'a' && v <= 'z': fallthrough case v >= 'A' && v <= 'Z': count.c += 1 case v == ' ': count.spance += 1 case v >= '0' && v <= '9': count.n += 1 default: count.other += 1 } } } fmt.Printf("char num :%d \nspance num :%d\nnumber num:%d\nother num :%d\n", count.c, count.spance, count.n, count.other) }
func main() { test()
} ------------------------- read to the end of file char num :547 spance num :115 number num:11 other num :330