2018-10-18 Go语言第一天

package main

import "fmt"
import "math"
import "math/cmplx"

var aa = 3 // 包内部变量
var ss = "kkk"
// bb := true  函数外面不可以用 :=

var (
  cc = 3  // 注意这里没有, 号
  dd = 5
)

func variableZero() {
  var a int
  var s string
  fmt.Printf("%d %q\n", a, s)
}

func variableInitialValue() {
  var a, b int = 3, 4
  var s string = "abc"
  fmt.Println(a, b, s)
}

func variableTypeDeuction() {
  var a, b, c, s = 3, 4, true, "Def"
  fmt.Println(a, b, c, s)
}

func variableShorter() {
  a, b, c, s := 3, 4, true, "def"
  fmt.Println(a, b, c, s)
}

func euler() {
  c := 3 + 4i
  fmt.Println(cmplx.Abs(c))
  fmt.Println(cmplx.Pow(math.E, 1i * math.Pi) + 1)
  fmt.Printf("%.3f\n", cmplx.Exp(1i * math.Pi) + 1)
}

func triangle() {
  var a, b int = 3, 4
  var c int
  c = int(math.Sqrt(float64(a * a + b * b)))
  fmt.Println(c)
}

func consts() {
  const filename = "abc.txt"
  const a, b = 3, 4
  var c int
  c = int(math.Sqrt(a*a + b*b))
  fmt.Println(filename, c)
}

func enums() {
  // const (
  //   cpp = 0
  //   java = 1
  //   python = 2
  //   golang = 3
  // )
  const (
    cpp = iota
    java
    _
    python
    golang
    javascript
  )
  fmt.Println(cpp, javascript, python, golang)

  const (
    b = 1 << (10 * iota)
    kb
    mb
    gb
    tb
    pb
  )
  fmt.Println(b, kb, mb, gb, tb, pb)
}

func main() {
  fmt.Println("Hello World")
  variableZero()
  variableInitialValue()
  variableTypeDeuction()
  variableShorter()
  fmt.Println(aa, ss, cc, dd)
  euler()
  triangle()
  consts()
  enums()
}

你可能感兴趣的:(2018-10-18 Go语言第一天)