Q1 从命令行终端中输入一个英文月份,然后输出这个月份在今年有多少天
    input: go run days-in-month.go april
    output: 30

    1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "strings"
    6. "time"
    7. )
    8. func main() {
    9. if len(os.Args) != 2 {
    10. fmt.Println("Give me a month name")
    11. return
    12. }
    13. year := time.Now().Year()
    14. leap := year%4 == 0 && (year%100 != 0 || year%400 == 0)
    15. days, month := 28, os.Args[1]
    16. switch strings.ToLower(month) {
    17. case "april", "june", "september", "november":
    18. days = 30
    19. case "january", "march", "may", "july",
    20. "august", "october", "december":
    21. days = 31
    22. case "february":
    23. if leap {
    24. days = 29
    25. }
    26. default:
    27. fmt.Printf("%q is not a month.\n", month)
    28. return
    29. }
    30. fmt.Printf("%q has %d days.\n", month, days)
    31. }

    Q2 从终端输入+-*/% 和一个数字,输出该数字的矩阵。
    go run dynamic-table.go + 4

    1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "strconv"
    6. "strings"
    7. )
    8. const (
    9. ValidOps = "%+-*/"
    10. UsageMsg = "Usage:[op="+ValidOps+"] [size]"
    11. sizeMissingMsg = "Size is missing"
    12. InvalidOpMsg =`Invalid operator. Valid ops one of:` + ValidOps
    13. InvalidOp = -1
    14. )
    15. func main(){
    16. args := os.Args
    17. switch len(args[1:]){
    18. case 1:
    19. fmt.Println(sizeMissingMsg)
    20. fallthrough
    21. case 0:
    22. fmt.Println(UsageMsg)
    23. return
    24. }
    25. indexNum := strings.IndexAny(args[1],"+-*/%")
    26. if indexNum < 0 {
    27. fmt.Println(InvalidOpMsg)
    28. return
    29. }
    30. size,err :=strconv.Atoi(args[2])
    31. if err != nil || size < 0 {
    32. fmt.Println(InvalidOpMsg)
    33. return
    34. }
    35. fmt.Printf("%5s",args[1])
    36. for i := 0;i <=size;i++{
    37. fmt.Printf("%5d",i)
    38. }
    39. fmt.Println()
    40. for i := 0; i<=size;i++{
    41. fmt.Printf("%5d",i)
    42. for j :=0;j<=size;j++{
    43. var res int
    44. switch args[1]{
    45. case "+":
    46. res = i+j
    47. case "-":
    48. res = i -j
    49. case "*":
    50. res = i * j
    51. case "/":
    52. if j !=0 {
    53. res=i/j
    54. }
    55. case "%":
    56. if j != 0{
    57. res = i % j
    58. }
    59. }
    60. fmt.Printf("%5d", res)
    61. }
    62. fmt.Println()
    63. }
    64. }

    Q3 随机猜数字游戏

    1. package main
    2. import (
    3. "fmt"
    4. "math/rand"
    5. "os"
    6. "strconv"
    7. "time"
    8. )
    9. const (
    10. maxTurns = 5 // less is more difficult
    11. usage = `Welcome to the Lucky Number Game! 🍀
    12. The program will pick %d random numbers.
    13. Your mission is to guess one of those numbers.
    14. The greater your number is, harder it gets.
    15. Wanna play?
    16. `
    17. )
    18. func main() {
    19. rand.Seed(time.Now().UnixNano())
    20. args := os.Args[1:]
    21. if len(args) != 2 {
    22. // fmt.Println("Pick a number.")
    23. fmt.Printf(usage, maxTurns)
    24. return
    25. }
    26. var verbose bool
    27. if args[0]=="-v"{
    28. verbose=true
    29. }
    30. guess, err := strconv.Atoi(args[1])
    31. if err != nil {
    32. fmt.Println("Not a number.")
    33. return
    34. }
    35. if guess < 0 {
    36. fmt.Println("Please pick a positive number.")
    37. return
    38. }
    39. for turn := 0; turn < maxTurns; turn++ {
    40. n := rand.Intn(guess + 1)
    41. if verbose {
    42. fmt.Printf("%d ", n)
    43. }
    44. if n == guess {
    45. switch rand.Intn(3){
    46. case 0:
    47. fmt.Print("🎉 YOU WIN!\n")
    48. case 1:
    49. fmt.Print("🎉 YOU'RE AWESOME! \n")
    50. case 2:
    51. fmt.Print("🎉 PERFECT! \n")
    52. }
    53. return
    54. }
    55. }
    56. fmt.Println("☠️ YOU LOST... Try again?")
    57. }

    Q4 使用lable + continue or break 跳出循环的操作
    break 用来终止 for , select , switch 语句,且仅能用在有这三个语句的地方
    continue 只用在 for 语句中,跳过当前循环执行下一次语句。

    1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "strings"
    6. )
    7. const corps = "lazy cat jumps again and again and again"
    8. func main() {
    9. words := strings.Fields(corps)
    10. query := os.Args[1:]
    11. querys:
    12. for _, q := range query {
    13. search:
    14. for i, w := range words {
    15. switch q {
    16. case "and", "or", "the":
    17. break search
    18. }
    19. if q == w {
    20. fmt.Printf("#%-2d:find the words %q\n", i+1, w)
    21. continue querys
    22. }
    23. }
    24. }
    25. }

    Q5 路径搜寻,给定某个路径,判断是否在PATH路径中

    1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "path/filepath"
    6. "strings"
    7. )
    8. func main() {
    9. path := os.Getenv("PATH")
    10. fmt.Println(path)
    11. words := filepath.SplitList(path)
    12. fmt.Println(words)
    13. query := os.Args[1:]
    14. for _, q := range query {
    15. for i, w := range words {
    16. q, w = strings.ToLower(q), strings.ToLower(w)
    17. if strings.Contains(w, q) {
    18. fmt.Printf("#%-2d:%q\n", i+1, w)
    19. }
    20. }
    21. }
    22. }

    Q6 随机字符串,可以实现一个在缓冲等待的动态符号,这个我还是很喜欢
    最后ctrl + c 结束该程序

    1. package main
    2. import (
    3. "fmt"
    4. "math/rand"
    5. "time"
    6. )
    7. func main() {
    8. var c string
    9. for {
    10. switch rand.Intn(4) {
    11. case 0:
    12. c = "\\"
    13. case 1:
    14. c = "/"
    15. case 2:
    16. c = "|"
    17. case 3:
    18. c = "-"
    19. }
    20. fmt.Printf("\r%s Please Wait. Processing....", c)
    21. time.Sleep(time.Millisecond * 250)
    22. }
    23. }