1. package main
    2. import (
    3. "fmt"
    4. "math"
    5. "math/cmplx"
    6. )
    7. var (
    8. aa = 3
    9. ss = "kkk"
    10. bb = true
    11. )
    12. func variableZeroValue() {
    13. var a int
    14. var s string
    15. fmt.Printf("%d %q\n", a, s)
    16. }
    17. func variableInitialValue() {
    18. var a, b int = 3, 4
    19. var s string = "abc"
    20. fmt.Println(a, b, s)
    21. }
    22. func variableTypeDeduction() {
    23. var a, b, c, s = 3, 4, true, "def"
    24. fmt.Println(a, b, c, s)
    25. }
    26. func variableShorter() {
    27. a, b, c, s := 3, 4, true, "def"
    28. b = 5
    29. fmt.Println(a, b, c, s)
    30. }
    31. func euler() {
    32. //复数
    33. c := 3 + 4i
    34. fmt.Println(cmplx.Abs(c))
    35. fmt.Printf("%.3f\n",
    36. cmplx.Exp(1i*math.Pi)+1)
    37. }
    38. func triangle() {
    39. var a, b int = 3, 4
    40. fmt.Println(calcTriangle(a, b))
    41. }
    42. func calcTriangle(a, b int) int {
    43. var c int
    44. c = int(math.Sqrt(float64(a*a + b*b)))
    45. return c
    46. }
    47. func consts() {
    48. const (
    49. filename = "abc.txt"
    50. a, b = 3, 4
    51. )
    52. var c int
    53. c = int(math.Sqrt(a*a + b*b))
    54. fmt.Println(filename, c)
    55. }
    56. func enums() {
    57. const (
    58. cpp = iota
    59. _
    60. python
    61. golang
    62. javascript
    63. )
    64. const (
    65. b = 1 << (10 * iota)
    66. kb
    67. mb
    68. gb
    69. tb
    70. pb
    71. )
    72. fmt.Println(cpp, javascript, python, golang)
    73. fmt.Println(b, kb, mb, gb, tb, pb)
    74. }
    75. func main() {
    76. fmt.Println("Hello world")
    77. //变量
    78. variableZeroValue()
    79. variableInitialValue()
    80. variableTypeDeduction()
    81. variableShorter()
    82. fmt.Println(aa, ss, bb)
    83. //欧拉公式
    84. euler()
    85. //强制类型转换
    86. triangle()
    87. //常量
    88. consts()
    89. //枚举
    90. enums()
    91. }

    image.jpeg