feat(Go-StudyExample): 新增 网络及端口测试、传值和传址的比较、切片测试、Map测试、通道测试、测试init调用、测试error、defer关键字、recover测试、测试携程的异常捕获、矩阵旋转

This commit is contained in:
huangzj
2020-07-13 17:02:04 +08:00
parent f5894a2680
commit 9ad10e8cd9
12 changed files with 514 additions and 13 deletions
+22 -13
View File
@@ -8,26 +8,35 @@ package main
import (
"Go-StudyExample/example"
"Go-StudyExample/example/originGoLanguage"
_ "Go-StudyExample/example/originGoLanguage/init"
"Go-StudyExample/example/problem"
)
func main() {
////--------------------------测试mapStructure的功能-----
////这边的四种使用方式差别感觉不是很大...
//example.MapStructureTestFunc()
//example.MapStructureTestFunc1()
//example.MapStructureTestFunc2()
//example.MapStructureTestFunc3()
//
//--------------------------测试mapStructure的功能-----
//这边的四种使用方式差别感觉不是很大...
example.MapStructureTestFunc()
example.MapStructureTestFunc1()
example.MapStructureTestFunc2()
example.MapStructureTestFunc3()
////--------------------------测试json包的转换功能
//example.JsonMarshalTest()
//example.JsonUnmarshalTest()
//
example.JsonMarshalTest()
example.JsonUnmarshalTest()
////克隆测试
//example.TestClone()
//
example.TestClone()
////二维码测试
//example.QrCodeTest()
example.QrCodeTest()
//图片测试
example.ImageTest()
//go原生语言测试
originGoLanguage.TestOriginLang()
//矩阵旋转
problem.MatrixRotationTest()
}
+33
View File
@@ -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
}
+133
View File
@@ -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")
}
+37
View File
@@ -0,0 +1,37 @@
/*
* @Author : huangzj
* @Time : 2020/7/13 14:24
* @Descriptiongo运行时错误测试
*/
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
}
+39
View File
@@ -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)
}
}
+48
View File
@@ -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()
}
+42
View File
@@ -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
}
+36
View File
@@ -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)
}
+14
View File
@@ -0,0 +1,14 @@
package init
import "fmt"
type InitGo struct {
}
func Test() {
fmt.Println(" ")
}
func init() {
fmt.Println("i am init func")
}
+70
View File
@@ -0,0 +1,70 @@
/*
* @Author : huangzj
* @Time : 2020/7/13 15:08
* @Description:矩阵旋转问题解决(这个是之前在2048游戏里面)
*/
package problem
import "fmt"
const (
SIZE = 4
)
func MatrixRotationTest() {
//矩阵旋转.....
e := [SIZE][SIZE]int{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}
MatrixRotation(e)
MatrixRotationAnti(e)
MatrixRotationHalfCircle(e)
MatrixRotationAntiHalfCircle(e)
}
//矩阵旋转-逆时针旋转90度
func MatrixRotationAnti(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
var temp [SIZE][SIZE]int
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
temp[SIZE-j-1][i] = matrix[i][j]
}
}
fmt.Println(temp)
return temp
}
//顺时针旋转90度
func MatrixRotation(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
var temp [SIZE][SIZE]int
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
temp[j][SIZE-1-i] = matrix[i][j]
}
}
fmt.Println(temp)
return temp
}
//矩阵逆时针旋转180度 --顺逆时针结果一致
func MatrixRotationAntiHalfCircle(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
var temp [SIZE][SIZE]int
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
temp[SIZE-1-i][SIZE-1-j] = matrix[i][j]
}
}
fmt.Println(temp)
return temp
}
//矩阵顺时针旋转180度
func MatrixRotationHalfCircle(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
var temp [SIZE][SIZE]int
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
temp[SIZE-1-i][SIZE-1-j] = matrix[i][j]
}
}
fmt.Println(temp)
return temp
}
+3
View File
@@ -11,6 +11,9 @@ json与struct之间转换处理工具使用示例
2020/7/8 : 添加两种二维码生成使用示例,添加两种图片压缩方式、添加水印示例,添加文字到图片
2020/7/13 : 新增 网络及端口测试、传值和传址的比较、切片测试、Map测试、通道测试、测试init调用、测试error、defer关键字、recover测试、测试携程的异常捕获、矩阵旋转
# mod vendor模式加载包
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到