golang的变量声明

基本格式
var identifier [type] [= value]

例子

var a int
var b string
var c bool
var d int = 8
var e string = "hello"
var f = "hello"

例子2

var (
  a int
  b string
  c bool
  d int = 8
  e string = "hello"
)

这个就跟golang的package引入类似,可以用括号将多个项目括起来,这样就只用写一个var或者import。

例子3

a := 20
golang的常量声明

基本格式
const identifier [type] = value

例子

const a int = 10
const b = 10
const c string = "233"
const d = "233"

例子2

const (
  a int = 100
  b = 10
  c string = "233"
  d = "233"
)

例子3

const (
  a = 100
  b 
  c = "233"
  d
)
bdb = 100d = "233"

例子4

const (
  a = iota  //0
  b         //1
  c = 234   //234
  d = iota  //3
  e         //4
)
iota
golang的基本类型

string

stringstring"`
var a = "123"
var b = `\n123`      //等同于C#中 var b = @"\n123"
string

取字符串长度

var a = "123"
len(a)

格式化输入到字符串

var a = 123
var c = fmt.Sprintf("%d",a)

字符串分割

var a = "123;234;345"
var aSlice = strings.Split(a,";")

字符串存在

var a = "123"
var aBool = strings.Contains(a,"2")

前后缀判断

var a = "pre123suf"
var aPreBool = strings.HasPrefix(a,"pre")
var aSufBool = strings.HasSuffix(a,"suf")

字符串查找

var a = "1234561"
var index = strings.Index(a,"1")
index = strings.LastIndex(a,"1")

字符串拼接

var strArr []string = {"123","234","345"}
var result = strings.Join(strArr, ";")
string

底层是数组,可以和byte数组进行转换

var str = "123456789"
var c = str[0]  //使用下标访问元素
for index,val := range str {
  fmt.Printf("index = %d, value = %c", index, val)
}
str[0] = '0'
var str = "123"
strSlice = []byte(str)
strSlice[0] = '0'
str =  string(strSlice)

字符串是由byte字节存储,所以字符串的长度是byte数组的长度。如果字符串中有中文,字符串长度会和字的个数不一致。

var str = "Hello, 中文"
fmt.Printf("length of str = %d", len(str))   //length of str = 13
runeSlice = []rune(str)
fmt.Printf("length of str = %d", len(runeSlice))   //length of str = 9

时间和日期类型

获取当前时间

now := time.Now()

获取时间戳

timeStamp := time.Now().Unix()

时间和时间戳转换

now := time.Now()
timeStamp := now.Unix()
now = time.Unix(timeStamp,0)

定时器

ticker := time.Tick(time.Second)
for i:= range ticker{
  fmt.Printf("%v\n", i)
}

时间常量

const (
  Nanosecond Duration
  Microsecond = 1000 * Nacosecond
  Millisecond = 1000 * Microsecond
  Second = 1000 * Millisecond
)

格式化

now := time.Now()
now.Format("2006/01/02 15:04:05" )  //这时间是golang的诞生时间,只能写这个
流程控制

if-else

和c系列的if-else非常类似,只是可以再一开始声明一个变量,而且不用括号

a := 10
if a > 5 {
  fmt.Printfln("great than 5")
}
else {
  fmt.Printfln("less than 5")
}
fmt.Printf("value = %d", a)
if a := 10; a > 5 {
  fmt.Printfln("great than 5")
}
else {
  fmt.Printfln("less than 5")
}
fmt.Printf("value = %d", a)  //非法。a只能在if的范围内使用

for

循环只有for,没有while。
基本用法

for i := 0; i < 10; i++ {
  
}

while用法

for i < 10 {
}

无限循环

for {
}

range遍历

for _, val := range b {
  sum += val
}

switch

基本用法,不需要跟break,默认就是有break的情况。

a := 10
switch a{
  case 1:
    //do something
  case 2:
    //do something
   default:
    //do something
}

和if一样,可以在使用时声明变量

switch a := 10; a{
  case 1:
    //do something
  case 2:
    //do something
   default:
    //do something
}

可以多个case放在一起

switch a := 10; a{
  case 1, 3, 5, 7, 9:
    //do something
  case 2, 4, 6, 8, 10:
    //do something
   default:
    //do something
}

可以不写固定值,而是用条件控制

a := 10
switch {
  case a < 5:
    //do something
  case a > 5:
    //do something
   default:
    //do something
}

可以执行多个case

a := 10
switch {
  case a < 5:
    //do something
    fallthrough
  case a <10:
    //do something
   default:
    //do something
}
函数

基本格式

func functionName([parameterName type]) [returnType]{
}

连续的相同类型的参数,可以只写一次类型

func add(a, b int) int {
  return a + b
}
_
func calc(a, b int) (int, int){
  sum := a + b
  sub := a - b
  return sum, sub
}

func calc1(a, b int) (sum, sub int) {
    sum = a + b
    sub = a - b
    return
}

aa, _ := calc(a, b)

可变参数

func calc(b ...int) int {
    sum := 0
    for _, val := range b {
        sum += val
    }
    return sum
}
defer
func testDefer() {
    defer fmt.Println("first defer")
    defer fmt.Println("second defer")
    defer fmt.Println("third defer")
    sam = "sam"
    defer fmt.Println("hello %s", sam)
    sam = "peter"
    fmt.Println("hello %s", sam)
}
//输出结果:
//hello peter
//hello sam
//third defer
//second defer
//first defer

函数可以存储在变量中。类似于函数指针或者delegate。可以声明匿名函数。

func func1(name string) {
  fmt.Printf("Hello %s\n", name)
}

func test() {
  f1 := func1
  f1("peter")
  f1 = func(name string){
    fmt.Printf("Welcome %s\n", name)
  }
  f1("peter")
}
//输出:
//Hello peter
//Welcome peter