分支switch

    1. package main
    2. import (
    3. "fmt"
    4. "io/ioutil"
    5. )
    6. func grade(score int) string {
    7. g := ""
    8. switch {
    9. case score < 0 || score > 100:
    10. panic(fmt.Sprintf(
    11. "Wrong score: %d", score))
    12. case score < 60:
    13. g = "F"
    14. case score < 80:
    15. g = "C"
    16. case score < 90:
    17. g = "B"
    18. case score <= 100:
    19. g = "A"
    20. }
    21. return g
    22. }
    23. func main() {
    24. // If "abc.txt" is not found,
    25. // please check what current directory is,
    26. // and change filename accordingly.
    27. const filename = "abc.txt"
    28. if contents, err := ioutil.ReadFile(filename); err != nil {
    29. fmt.Println(err)
    30. } else {
    31. fmt.Printf("%s\n", contents)
    32. }
    33. //switch
    34. fmt.Println(
    35. grade(0),
    36. grade(59),
    37. grade(60),
    38. grade(82),
    39. grade(99),
    40. grade(100),
    41. // Uncomment to see it panics.
    42. grade(-3),
    43. )
    44. }

    循环分支

    1. package main
    2. import (
    3. "bufio"
    4. "fmt"
    5. "io"
    6. "os"
    7. "strconv"
    8. "strings"
    9. )
    10. func convertToBin(n int) string {
    11. result := ""
    12. for ; n > 0; n /= 2 {
    13. lsb := n % 2
    14. result = strconv.Itoa(lsb) + result
    15. }
    16. return result
    17. }
    18. func printFile(filename string) {
    19. file, err := os.Open(filename)
    20. if err != nil {
    21. panic(err)
    22. }
    23. printFileContents(file)
    24. }
    25. func printFileContents(reader io.Reader) {
    26. scanner := bufio.NewScanner(reader)
    27. for scanner.Scan() {
    28. fmt.Println(scanner.Text())
    29. }
    30. }
    31. func forever() {
    32. for {
    33. fmt.Println("abc")
    34. }
    35. }
    36. func main() {
    37. fmt.Println("convertToBin results:")
    38. fmt.Println(
    39. convertToBin(5), // 101
    40. convertToBin(13), // 1101
    41. convertToBin(72387885),
    42. convertToBin(0),
    43. )
    44. fmt.Println("abc.txt contents:")
    45. printFile("basic/branch/abc.txt")
    46. fmt.Println("printing a string:")
    47. s := `abc"d"
    48. kkkk
    49. 123
    50. p`
    51. printFileContents(strings.NewReader(s))
    52. // Uncomment to see it runs forever
    53. // forever()
    54. }

    函数

    1. package main
    2. import (
    3. "fmt"
    4. "math"
    5. "reflect"
    6. "runtime"
    7. )
    8. func eval(a, b int, op string) (int, error) {
    9. switch op {
    10. case "+":
    11. return a + b, nil
    12. case "-":
    13. return a - b, nil
    14. case "*":
    15. return a * b, nil
    16. case "/":
    17. q, _ := div(a, b)
    18. return q, nil
    19. default:
    20. return 0, fmt.Errorf(
    21. "unsupported operation: %s", op)
    22. }
    23. }
    24. func div(a, b int) (q, r int) {
    25. return a / b, a % b
    26. }
    27. func apply(op func(int, int) int, a, b int) int {
    28. p := reflect.ValueOf(op).Pointer()
    29. opName := runtime.FuncForPC(p).Name()
    30. fmt.Printf("Calling function %s with args "+
    31. "(%d, %d)\n", opName, a, b)
    32. return op(a, b)
    33. }
    34. //可变参数
    35. func sum(numbers ...int) int {
    36. s := 0
    37. for i := range numbers {
    38. s += numbers[i]
    39. }
    40. return s
    41. }
    42. func swap(a, b int) (int, int) {
    43. return b, a
    44. }
    45. func main() {
    46. fmt.Println("Error handling")
    47. if result, err := eval(3, 4, "x"); err != nil {
    48. fmt.Println("Error:", err)
    49. } else {
    50. fmt.Println(result)
    51. }
    52. q, r := div(13, 3)
    53. fmt.Printf("13 div 3 is %d mod %d\n", q, r)
    54. // 函数式编程
    55. fmt.Println("pow(3, 4) is:", apply(
    56. func(a int, b int) int {
    57. return int(math.Pow(
    58. float64(a), float64(b)))
    59. }, 3, 4))
    60. fmt.Println("1+2+...+5 =", sum(1, 2, 3, 4, 5))
    61. a, b := 3, 4
    62. a, b = swap(a, b)
    63. fmt.Println("a, b after swap is:", a, b)
    64. }

    image.jpeg