Go语言基础 发表于 2022-11-20 | 更新于: 2022-11-20 | 分类于 编程 | 本文总阅读量 次 字数统计: 179 | 阅读时长 ≈ 1 入门for与for-each for 12345678910package mainimport "fmt"func main() { var num = 10 for i := 0; i < num; i++ { fmt.Println(i) }} for-each 1234567891011package mainimport "fmt"func main() { var s = "12345" // _为索引,因为用不到,但range需要,因此使用临时变量 for _, i := range s { fmt.Println(string(i)) }} 计算重复行数123456789101112131415161718192021package mainimport ( "bufio" "fmt" "os")func main() { counts := make(map[string]int) input := bufio.NewScanner(os.Stdin) for input.Scan() { counts[input.Text()]++ } for line, n := range counts { if n > 1 { fmt.Printf("%d\t%s\n\n", n, line) } }} 从键盘获取输入1234567891011package mainimport "fmt"func main() { var a int var b int _, _ = fmt.Scanln(&a) _, _ = fmt.Scanln(&b) fmt.Println(a + b)} ----------- 本文结束 -----------