feat(Go-StudyExample): 新增 网络及端口测试、传值和传址的比较、切片测试、Map测试、通道测试、测试init调用、测试error、defer关键字、recover测试、测试携程的异常捕获、矩阵旋转
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:27
|
||||
* @Description:通道测试
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import "fmt"
|
||||
|
||||
func TestChannel() {
|
||||
|
||||
list := []int{0, 1, 45, -12, 33, 90, -22, 100}
|
||||
//通道的初始化
|
||||
c := make(chan int)
|
||||
|
||||
go sum(list[len(list)/2:], c)
|
||||
go sum(list[:len(list)/2], c)
|
||||
|
||||
//通道获取值的赋值
|
||||
x, y := <-c, <-c
|
||||
fmt.Println(x, y, x+y)
|
||||
//通道关闭
|
||||
close(c)
|
||||
}
|
||||
|
||||
func sum(list []int, c chan int) {
|
||||
sum := 0
|
||||
for _, i := range list {
|
||||
sum += i
|
||||
}
|
||||
c <- sum
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:37
|
||||
* @Description:测试defer关键字、recover
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDefer() {
|
||||
//defer会在所有函数执行完成之后才进行执行
|
||||
testDeferExe()
|
||||
//defer产生的值是在对应位置的值,后面的变化不会产生影响
|
||||
fmt.Println(testDeferValue())
|
||||
//defer的执行是先进后出
|
||||
testDeferExeOrder()
|
||||
testDeferRecover() //defer对异常的捕获
|
||||
testDeferRecoverOut() //外层异常捕获
|
||||
}
|
||||
|
||||
func TestCtripException() {
|
||||
fmt.Println("测试不进行捕获的情况")
|
||||
testNotDefer()
|
||||
|
||||
fmt.Println("测试进行捕获的情况")
|
||||
testDefer1()
|
||||
}
|
||||
|
||||
func testNotDefer() {
|
||||
go sayHello()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(5000)
|
||||
fmt.Println(i)
|
||||
}
|
||||
}
|
||||
|
||||
func testDefer1() {
|
||||
go sayHello1()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(5000)
|
||||
fmt.Println(i)
|
||||
}
|
||||
}
|
||||
|
||||
func sayHello1() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Println("错误信息", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func sayHello() {
|
||||
var map1 map[string]int
|
||||
//这里会发生异常
|
||||
fmt.Println(map1["say"])
|
||||
}
|
||||
|
||||
func testDeferRecover() {
|
||||
//多个panic只会捕获最后一个
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Println("错误信息是:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
panic1()
|
||||
panic2()
|
||||
panic3()
|
||||
|
||||
//一旦错误被捕获就不会继续运行
|
||||
fmt.Println("我会继续运行")
|
||||
|
||||
}
|
||||
|
||||
func panic3() {
|
||||
panic("panic3")
|
||||
}
|
||||
|
||||
func panic2() {
|
||||
panic("panic2")
|
||||
}
|
||||
|
||||
func panic1() {
|
||||
panic("panic1")
|
||||
}
|
||||
|
||||
func testDeferRecoverOut() {
|
||||
testDeferRecover()
|
||||
|
||||
fmt.Println("外层继续执行")
|
||||
}
|
||||
|
||||
func testDeferExe() {
|
||||
i := 0
|
||||
defer func() {
|
||||
fmt.Println(i)
|
||||
}()
|
||||
fmt.Println("i am after")
|
||||
}
|
||||
|
||||
func testDeferValue() int {
|
||||
i := 0
|
||||
defer func() {
|
||||
fmt.Println("first: ", i)
|
||||
}()
|
||||
|
||||
i++
|
||||
defer func() {
|
||||
fmt.Println("second: ", i)
|
||||
}()
|
||||
|
||||
i++
|
||||
return i
|
||||
}
|
||||
|
||||
func testDeferExeOrder() {
|
||||
defer func() {
|
||||
fmt.Println("first")
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
fmt.Println("second")
|
||||
}()
|
||||
|
||||
defer fmt.Println("third")
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:24
|
||||
* @Description:go运行时错误测试
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func TestError() {
|
||||
fmt.Println("测试创建一个新的错误")
|
||||
testNewError()
|
||||
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
func testNewError() {
|
||||
var fl1 float32 = 0
|
||||
|
||||
fl2, err := tError(fl1)
|
||||
fmt.Println(fl2, err)
|
||||
|
||||
var flx float32 = 2.2
|
||||
fl3, err1 := tError(flx)
|
||||
fmt.Println(fl3, err1)
|
||||
}
|
||||
|
||||
func tError(f float32) (float32, error) {
|
||||
if f == 0 {
|
||||
return 0, errors.New("错误")
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:22
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import "fmt"
|
||||
|
||||
func TestMap() {
|
||||
//没有初始化的是nil,不能进行赋值
|
||||
var countryMap1 map[string]string
|
||||
fmt.Println(countryMap1 == nil)
|
||||
|
||||
countryMap := make(map[string]string)
|
||||
countryMap["a"] = "aCountry"
|
||||
countryMap["b"] = "bCountry"
|
||||
countryMap["c"] = "cCountry"
|
||||
countryMap["d"] = "dCountry"
|
||||
|
||||
for key, value := range countryMap {
|
||||
fmt.Println(key + " is " + value)
|
||||
}
|
||||
|
||||
//获取Map的值
|
||||
mValue, ok := countryMap["a"]
|
||||
fmt.Println(ok, mValue)
|
||||
|
||||
mValue1, ok1 := countryMap["aaaa"]
|
||||
fmt.Println(ok1, mValue1)
|
||||
|
||||
//删除map中的元素
|
||||
delete(countryMap, "a")
|
||||
delete(countryMap, "b")
|
||||
for key, value := range countryMap {
|
||||
fmt.Println(key + " is " + value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 11:55
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func NetTest() {
|
||||
|
||||
//InterfaceAddrs 返回该系统的网络接口的地址列表。
|
||||
addr, _ := net.InterfaceAddrs()
|
||||
fmt.Println(addr)
|
||||
|
||||
//Interfaces 返回该系统的网络接口列表
|
||||
interfaces, _ := net.Interfaces()
|
||||
fmt.Println(interfaces)
|
||||
|
||||
//LookupAddr 查询某个地址,返回映射到该地址的主机名序列
|
||||
lt, _ := net.LookupAddr("www.alibaba.com")
|
||||
fmt.Println(lt)
|
||||
|
||||
//LookupCNAME函数查询name的规范DNS名(但该域名未必可以访问)。
|
||||
cname, _ := net.LookupCNAME("www.baidu.com")
|
||||
fmt.Println(cname)
|
||||
|
||||
//LookupHost函数查询主机的网络地址序列。
|
||||
host, _ := net.LookupHost("www.baidu.com")
|
||||
fmt.Println(host)
|
||||
|
||||
//LookupIP函数查询主机的ipv4和ipv6地址序列。
|
||||
ip, _ := net.LookupIP("www.baidu.com")
|
||||
fmt.Println(ip)
|
||||
|
||||
//函数将host和port合并为一个网络地址。一般格式为"host:port";如果host含有冒号或百分号,格式为"[host]:port"。
|
||||
//Ipv6的文字地址或者主机名必须用方括号括起来,如"[::1]:80"、"[ipv6-host]:http"、"[ipv6-host%zone]:80"。
|
||||
hp := net.JoinHostPort("127.0.0.1", "8080")
|
||||
fmt.Println(hp)
|
||||
|
||||
//函数将格式为"host:port"、"[host]:port"或"[ipv6-host%zone]:port"的网络地址分割为host或ipv6-host%zone和port两个部分。
|
||||
shp, port, _ := net.SplitHostPort("127.0.0.1:8080")
|
||||
fmt.Println(shp, " _ ", port)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:35
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
func TestOriginLang() {
|
||||
|
||||
//网络及端口测试
|
||||
NetTest()
|
||||
|
||||
//传值和传址的比较
|
||||
TestQuote()
|
||||
|
||||
//切片测试
|
||||
TestSlice()
|
||||
|
||||
//测试Map
|
||||
TestMap()
|
||||
|
||||
//通道测试
|
||||
TestChannel()
|
||||
|
||||
//测试init,只要在当前结构体中有引入对应的包,就会调用相应的init方法,具体可以查看 上面的import
|
||||
|
||||
//测试error的生成
|
||||
TestError()
|
||||
|
||||
//测试defer关键字
|
||||
TestDefer()
|
||||
|
||||
//测试携程的异常捕获 --- 好像有没有捕获,主函数都可以正常运行
|
||||
TestCtripException()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 13:43
|
||||
* @Description:传值和传址
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import "fmt"
|
||||
|
||||
func TestQuote() {
|
||||
x, y := "i am x", "i am y"
|
||||
|
||||
fmt.Println("回参的值2")
|
||||
x2, y2 := byValue(x, y)
|
||||
fmt.Println(x2)
|
||||
fmt.Println(y2)
|
||||
|
||||
fmt.Println("传值的方式(原始值)")
|
||||
fmt.Println(x)
|
||||
fmt.Println(y)
|
||||
|
||||
fmt.Println("回参的值")
|
||||
x1, y1 := byAddress(&x, &y)
|
||||
fmt.Println(x1)
|
||||
fmt.Println(y1)
|
||||
|
||||
fmt.Println("传引用的方式(原始值)")
|
||||
fmt.Println(x)
|
||||
fmt.Println(y)
|
||||
|
||||
}
|
||||
|
||||
func byAddress(i *string, i2 *string) (string, string) {
|
||||
*i2, *i = *i, *i2
|
||||
return *i, *i2
|
||||
}
|
||||
|
||||
func byValue(s string, s2 string) (string, string) {
|
||||
s, s2 = s2, s
|
||||
return s, s2
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @Author : huangzj
|
||||
* @Time : 2020/7/13 14:19
|
||||
* @Description:切片的测试
|
||||
*/
|
||||
|
||||
package originGoLanguage
|
||||
|
||||
import "fmt"
|
||||
|
||||
func TestSlice() {
|
||||
//定义切片
|
||||
var number []int
|
||||
fmt.Println(number)
|
||||
|
||||
number = append(number, 1)
|
||||
number = append(number, 2)
|
||||
number = append(number, 3)
|
||||
number = append(number, 4)
|
||||
|
||||
fmt.Println(len(number), cap(number), number)
|
||||
|
||||
//切片截取
|
||||
fmt.Println(number[0:1])
|
||||
fmt.Println(number[:2])
|
||||
fmt.Println(number[1:])
|
||||
|
||||
//初始化切片
|
||||
number1 := make([]int, len(number), 10)
|
||||
fmt.Println(len(number1), cap(number1), number1)
|
||||
|
||||
//复制切片
|
||||
copy(number1, number)
|
||||
fmt.Println(len(number1), cap(number1), number1)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package init
|
||||
|
||||
import "fmt"
|
||||
|
||||
type InitGo struct {
|
||||
}
|
||||
|
||||
func Test() {
|
||||
fmt.Println(" ")
|
||||
}
|
||||
|
||||
func init() {
|
||||
fmt.Println("i am init func")
|
||||
}
|
||||
Reference in New Issue
Block a user