go(一)基础知识

一、第一个程序

基本程序结构

package main // 包

import "fmt" // 引入依赖代码

// 功能实现
func main() { fmt.Println("hello world!") }

注意点:

1.应用程序入口

  • 必须是main包:package main
  • 必须是main方法:func main()
  • 文件名不一定是main.go

2.退出返回值

与其他主要编程语言的差异

  • Go中main函数不支持任何返回值
  • 通过os.Exit来返回状态

3.获取命令行参数

main函数不支持传入参数

func main()

在程序中直接通过os.Args获取命令行参数

编写测试文件

源码文件以_test结尾:xxx_test.go

测试方法名以Test开头:func TestXXX(t *testing.T){...}

package try_test

import "testing"

func TestFirstTry(t *testing.T)  {

    t.Log("My first try!")

}

结果

=== RUN   TestFirstTry
--- PASS: TestFirstTry (0.00s)
    first_test.go:7: My first try!
PASS

Fibonacci数列

package try

import (
    "fmt"
    "testing"
)

func TestFibList(t *testing.T)  {
    var a int = 1
    var b int = 1

    //var{
    // 可以省略int,类型推断
    //    a int = 1
    //    b int = 1
    //}
    // 类型推断,初试化
    //a := 1
    //b := 1
    fmt.Print(a, " ")
    for i:=0;i<5;i++{
        fmt.Print(" ", b)
        tmp:=a
        a=b
        b=tmp+a
    }
    fmt.Println()
}

二、变量、常量、数据类型和运算符

变量赋值

与其他主要编程语言的差异

赋值可以进行自动推断

在一个赋值语句中可以对多个变量进行同时赋值

func TestExchage(t *testing.T){
    a := 1
    b := 1
    //tmp := a
    //a = b
    //b = tmp
    a, b= b, a
    t.Log(a, b)
}

常量定义

与其他主要编程语言的差异

快速设置连续值

const (
    Monday = iota + 1
    Tuesday
    Wednesday
    Thrusday
    Friday
    Saturday
    Sunday
)

const (
    Open = 1 << iota
    Close
    Pending
) 

例子

package _const

import "testing"

const (
    Monday = iota + 1
    Tuesday
    Wednesday
)

const(
    Readable = 1<<iota
    Writable
    Executable
)

func TestConstantTry(t *testing.T)  {
    t.Log(Monday, Tuesday, Wednesday)
} // 1,2,3

func TestConstantTry1(t *testing.T)  {
    a := 7 // 0111
    t.Log(a&Readable==Readable, a&Writable==Writable, a&Executable==Executable)
} // true,true,true

数据类型

基本数据类型

类型转化

与其他主要编程语言的差异

1.Go语言不允许隐式类型转换

2.别名和原有类型也不能进行隐式类型转换

package type_test

import "testing"

type MyInt int64

func TestImplict(t *testing.T)  {
    var a int = 1
    var b int64
    //b = a // 不支持隐式类型转换
    b = int64(a)
    t.Log(a, b)
    var c MyInt
    // c = b // 别名的隐式类型转换也不支持
    c = MyInt(b)
    t.Log(a, b, c)
}

类型的预定义值

指针类型

与其他主要编程语言的差异

1.不支持指针运算

func TestPoint(t *testing.T)  {
    a := 1
    aPtr := &a
    t.Log(a, aPtr)
    t.Logf("%T %T", a, aPtr)
    //aPtr = aPtr + 1 // 出错,不支持指针运算
}


结果

=== RUN TestPoint
--- PASS: TestPoint (0.00s)
type_test.go:22: 1 0xc04205e290
type_test.go:23: int *int
PASS

2.string是值类型,其默认初试化值为空字符串,而不是nil

func TestString(t *testing.T)  {
    var s string
    t.Log("*"+s+"*")
    t.Log(len(s))
}

结果
=== RUN   TestString
--- PASS: TestString (0.00s)
    type_test.go:29: **
    type_test.go:30: 0
PASS

运算符

算术运算符

比较运算符

用==比较数组

  • 相同维数且含有相同个数元素的数组才可以比较
  • 每个元素都相同的才相等

逻辑运算符

位运算符

与其他主要编程语言的差异

三、编写结构化程序

循环

和其他主要编程语言的差异

 示例

实例

func TestWhileLoop(t *testing.T)  {
    n := 0
    for n < 5{
        t.Log(n)
        n++
    }
}


结果
=== RUN   TestWhileLoop
--- PASS: TestWhileLoop (0.00s)
    type_test.go:36: 0
    type_test.go:36: 1
    type_test.go:36: 2
    type_test.go:36: 3
    type_test.go:36: 4
PASS

if条件

 与其他编程语言的差异

  • condition表达式结果必须为布尔值
  • 支持变量赋值
if var declaration; condition {
    // code to be executed if condition is true
}
func TestIfMultiSec(t *testing.T)  {
    if a := 1 == 1;a {
        t.Log("1==1")
    }
}

// a是true,所以执行括号里内容


结果
=== RUN   TestIfMultiSec
--- PASS: TestIfMultiSec (0.00s)
    type_test.go:43: 1==1
PASS

 switch条件

 与其他主要编程语言的差异

  • 条件表达式不限制为常量或整数
  • 单个case中,可以出现多个结果选项,使用逗号分隔
  • 与C等规则相反,Go语言不需要用break来明确退出一个case
  • 可以不设定switch之后的条件表达式,在此情况下,整个switch结构与多个if...else...的逻辑作用等同

可以有多个选项

func TestSwitchMultiCase(t *testing.T)  {
    for i := 0; i < 5; i++{
        switch i {
        case 0, 2:
            t.Log("Even")
        case 1,3:
            t.Log("Odd")
        default:
            t.Log("It is not 0-3")
        }
    }
}

相当于if...else...使用

func TestSwitchCaseCondition(t *testing.T)  {
    for i := 0; i < 5; i++ {
        switch  {
        case i%2 == 0:
            t.Log("Even")
        case i%2 == 1:
            t.Log("Odd")
        default:
            t.Log("unknow")
        }
    }
}

四、数组和切片

 数组的声明

 数组元素遍历

与其他主要编程语言的差异

数组截取

切片内部结构

切片声明

切片共享存储结构

数组vs切片

  • 容量是否可伸缩
  • 是否可以进行比较

五、Map

 Map声明

Map元素访问

与其他主要编程语言的差异

Map遍历

 Map与工厂模式

  • Map的value可以是一个方法
  • 与Go的Dock type接口方式一起,可以方便实现单一方法对象的工厂模式

实现Set

 

六、字符串

 与其他主要编程语言的差异

 Unicode UTF8

编码与存储

常用字符串函数

---恢复内容结束---

猜你喜欢

转载自www.cnblogs.com/aidata/p/11729979.html