diff --git a/Main.go b/Main.go index b1e3379..46f6952 100644 --- a/Main.go +++ b/Main.go @@ -7,36 +7,13 @@ 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() - - ////--------------------------测试json包的转换功能 - example.JsonMarshalTest() - example.JsonUnmarshalTest() - - ////克隆测试 - example.TestClone() - - ////二维码测试 - example.QrCodeTest() - - //图片测试 - example.ImageTest() //go原生语言测试 originGoLanguage.TestOriginLang() - //矩阵旋转 - problem.MatrixRotationTest() } diff --git a/example/Assert.go b/example/Assert.go index cfed950..39bb63b 100644 --- a/example/Assert.go +++ b/example/Assert.go @@ -6,7 +6,7 @@ package example -func assert(err error) { +func Assert(err error) { if err != nil { panic(err) } diff --git a/example/cupMessage/CpuMessage_test.go b/example/cupMessage/CpuMessage_test.go index c59624b..71b5853 100644 --- a/example/cupMessage/CpuMessage_test.go +++ b/example/cupMessage/CpuMessage_test.go @@ -18,13 +18,6 @@ import ( "github.com/shirou/gopsutil/mem" ) -func TestGetCpuMessage(t *testing.T) { - DiskCheck() - OSCheck() - CPUCheck() - RAMCheck() -} - const ( B = 1 KB = 1024 * B @@ -33,7 +26,7 @@ const ( ) //服务器硬盘使用量 -func DiskCheck() { +func TestDiskCheck(t *testing.T) { u, _ := disk.Usage("/") usedMB := int(u.Used) / MB usedGB := int(u.Used) / GB @@ -44,12 +37,12 @@ func DiskCheck() { } //OS -func OSCheck() { +func TestOSCheck(t *testing.T) { fmt.Printf("goOs:%s,compiler:%s,numCpu:%d,version:%s,numGoroutine:%d\n", runtime.GOOS, runtime.Compiler, runtime.NumCPU(), runtime.Version(), runtime.NumGoroutine()) } //CPU 使用量 -func CPUCheck() { +func TestCPUCheck(t *testing.T) { cores, _ := cpu.Counts(false) cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true) @@ -69,7 +62,7 @@ func CPUCheck() { } //内存使用量 -func RAMCheck() { +func TestRAMCheck(t *testing.T) { u, _ := mem.VirtualMemory() usedMB := int(u.Used) / MB totalMB := int(u.Total) / MB diff --git a/example/CloneExample.go b/example/goClone/Clone_test.go similarity index 89% rename from example/CloneExample.go rename to example/goClone/Clone_test.go index 655259e..71258e1 100644 --- a/example/CloneExample.go +++ b/example/goClone/Clone_test.go @@ -10,27 +10,16 @@ import ( "Go-StudyExample/entity" "Go-StudyExample/util" "fmt" + "testing" ) -func TestClone() { - fmt.Print("\n\n\n") - fmt.Print(" Go语言深克隆的测试:\n\n") - - testJustBasicType() - - testHasPointType() - +func TestClone(t *testing.T) { fmt.Println("由此可见其实Go语言对于内存的分配,相对对象是独一份,对于对象的基本类型和值类型(结构体),应该是在其内部开辟了内存空间进行存储,而对于" + "指针类型来说,直接使用它的地址(指针),不对结构体进行复制") - fmt.Println("") - - testDeepCloneByEncode() - - testDeepCloneByJsonPackage() - } -func testDeepCloneByJsonPackage() { +//json包处理深度克隆 +func TestDeepCloneByJsonPackage(t *testing.T) { rule := entity.SynthesisRule{ SynthesisId: "112", SynthesisNum: 0, @@ -45,7 +34,8 @@ func testDeepCloneByJsonPackage() { } } -func testDeepCloneByEncode() { +//序列化方式进行深度克隆 +func TestDeepCloneByEncode(t *testing.T) { rule := entity.SynthesisRule{ SynthesisId: "112", SynthesisNum: 0, @@ -63,7 +53,8 @@ func testDeepCloneByEncode() { } } -func testJustBasicType() { +//结构体带基本类型 +func TestJustBasicType(t *testing.T) { rule := entity.SynthesisRule{ SynthesisId: "112", SynthesisNum: 0, @@ -83,7 +74,8 @@ func testJustBasicType() { } -func testHasPointType() { +//结构体带指针 +func TestHasPointType(t *testing.T) { cmp := entity.SynthesisRuleCmp{ SynthesisRule: &entity.SynthesisRule{ SynthesisId: "112", @@ -104,7 +96,8 @@ func testHasPointType() { } } -func testHasPointTypeList() { +//结构体带切片 +func TestHasPointTypeSlice(t *testing.T) { cmp := entity.SynthesisRuleCmp{ SynthesisRule: &entity.SynthesisRule{ diff --git a/example/ImageExample.go b/example/image/Image_test.go similarity index 99% rename from example/ImageExample.go rename to example/image/Image_test.go index a56730b..82eeded 100644 --- a/example/ImageExample.go +++ b/example/image/Image_test.go @@ -18,6 +18,7 @@ import ( "io/ioutil" "log" "os" + "testing" ) const ( @@ -33,7 +34,7 @@ const ( wordPath = "example/image/wordPath.png" //携带文字的图片 ) -func ImageTest() { +func TestImage(t *testing.T) { AddWordToImg() //设置文字到图片 diff --git a/example/JsonTest.go b/example/jsonPackage/Json_test.go similarity index 86% rename from example/JsonTest.go rename to example/jsonPackage/Json_test.go index e0e8907..e4027a1 100644 --- a/example/JsonTest.go +++ b/example/jsonPackage/Json_test.go @@ -4,18 +4,19 @@ * @Description: */ -package example +package jsonPackage import ( "encoding/json" "fmt" + "testing" "time" ) type Time time.Time const ( - timeFormart = "2006-01-02 15:04:05" + timeFormat = "2006-01-02 15:04:05" ) //-------------------结构体---------------- @@ -44,7 +45,7 @@ type json1 struct { /* * 测试序列化功能 */ -func JsonMarshalTest() { +func TestJsonMarshalTest(t *testing.T) { j := json1{ Name: "name", Value: "value", @@ -73,7 +74,7 @@ func JsonMarshalTest() { /* * 测试序列化功能 */ -func JsonUnmarshalTest() { +func TestJsonUnmarshalTest(t *testing.T) { src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}` p := new(Person) err := json.Unmarshal([]byte(src), &p) @@ -89,7 +90,7 @@ func JsonUnmarshalTest() { //实现该方法,实现对应的时间处理,json应该是没有支持时间处理的 func (t *Time) UnmarshalJSON(data []byte) (err error) { - now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local) + now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local) *t = Time(now) return } @@ -98,13 +99,13 @@ func (t *Time) UnmarshalJSON(data []byte) (err error) { * 实现该方法,确定时间格式的输出 */ func (t Time) MarshalJSON() ([]byte, error) { - b := make([]byte, 0, len(timeFormart)+2) + b := make([]byte, 0, len(timeFormat)+2) b = append(b, '"') - b = time.Time(t).AppendFormat(b, timeFormart) + b = time.Time(t).AppendFormat(b, timeFormat) b = append(b, '"') return b, nil } func (t Time) String() string { - return time.Time(t).Format(timeFormart) + return time.Time(t).Format(timeFormat) } diff --git a/example/MapstructureExample.go b/example/mapstructure/Mapstructure_test.go similarity index 92% rename from example/MapstructureExample.go rename to example/mapstructure/Mapstructure_test.go index 19936d5..b384f62 100644 --- a/example/MapstructureExample.go +++ b/example/mapstructure/Mapstructure_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/goinggo/mapstructure" + "testing" ) //-----------------------json数据--------------------- @@ -31,7 +32,7 @@ type Items struct { //---------------------------------测试方法------------------------ -func MapStructureTestFunc() { +func TestMapStructureTestFunc(t *testing.T) { var te entity2.Entity m := make(map[string]interface{}) m["Num"] = 1 @@ -46,7 +47,7 @@ func MapStructureTestFunc() { fmt.Print(te.Num, " ", te.S, " ", te.T) } -func MapStructureTestFunc1() { +func TestMapStructureTestFunc1(t *testing.T) { var docMap map[string]interface{} _ = json.Unmarshal([]byte(document), &docMap) @@ -64,7 +65,7 @@ type NameDoc struct { Name string `jpath:"name"` } -func MapStructureTestFunc2() { +func TestMapStructureTestFunc2(t *testing.T) { sliceScript := []byte(document2) var sliceMap []map[string]interface{} @@ -80,7 +81,7 @@ func MapStructureTestFunc2() { fmt.Println(myslice[0], " ", myslice[1]) } -func MapStructureTestFunc3() { +func TestMapStructureTestFunc3(t *testing.T) { docScript := []byte(document1) var docMap map[string]interface{} _ = json.Unmarshal(docScript, &docMap) diff --git a/example/originGoLanguage/ChannelTest.go b/example/originGoLanguage/Channel_test.go similarity index 88% rename from example/originGoLanguage/ChannelTest.go rename to example/originGoLanguage/Channel_test.go index c54636d..06cc41c 100644 --- a/example/originGoLanguage/ChannelTest.go +++ b/example/originGoLanguage/Channel_test.go @@ -6,9 +6,12 @@ package originGoLanguage -import "fmt" +import ( + "fmt" + "testing" +) -func TestChannel() { +func TestChannel(t *testing.T) { list := []int{0, 1, 45, -12, 33, 90, -22, 100} //通道的初始化 diff --git a/example/originGoLanguage/DeferTest.go b/example/originGoLanguage/Defer_test.go similarity index 95% rename from example/originGoLanguage/DeferTest.go rename to example/originGoLanguage/Defer_test.go index a9442c6..a0bd38a 100644 --- a/example/originGoLanguage/DeferTest.go +++ b/example/originGoLanguage/Defer_test.go @@ -8,10 +8,11 @@ package originGoLanguage import ( "fmt" + "testing" "time" ) -func TestDefer() { +func TestDefer(t *testing.T) { //defer会在所有函数执行完成之后才进行执行 testDeferExe() //defer产生的值是在对应位置的值,后面的变化不会产生影响 @@ -22,7 +23,7 @@ func TestDefer() { testDeferRecoverOut() //外层异常捕获 } -func TestCtripException() { +func TestCtripException(t *testing.T) { fmt.Println("测试不进行捕获的情况") testNotDefer() diff --git a/example/originGoLanguage/ErrorTest.go b/example/originGoLanguage/Error_test.go similarity index 92% rename from example/originGoLanguage/ErrorTest.go rename to example/originGoLanguage/Error_test.go index b815c4c..3188f63 100644 --- a/example/originGoLanguage/ErrorTest.go +++ b/example/originGoLanguage/Error_test.go @@ -9,9 +9,10 @@ package originGoLanguage import ( "errors" "fmt" + "testing" ) -func TestError() { +func TestError(t *testing.T) { fmt.Println("测试创建一个新的错误") testNewError() diff --git a/example/originGoLanguage/MapTest.go b/example/originGoLanguage/Map_test.go similarity index 92% rename from example/originGoLanguage/MapTest.go rename to example/originGoLanguage/Map_test.go index ca13145..830e749 100644 --- a/example/originGoLanguage/MapTest.go +++ b/example/originGoLanguage/Map_test.go @@ -6,9 +6,12 @@ package originGoLanguage -import "fmt" +import ( + "fmt" + "testing" +) -func TestMap() { +func TestMap(t *testing.T) { //没有初始化的是nil,不能进行赋值 var countryMap1 map[string]string fmt.Println(countryMap1 == nil) diff --git a/example/originGoLanguage/NetTest.go b/example/originGoLanguage/Net_test.go similarity index 97% rename from example/originGoLanguage/NetTest.go rename to example/originGoLanguage/Net_test.go index 5e902f5..dd38d09 100644 --- a/example/originGoLanguage/NetTest.go +++ b/example/originGoLanguage/Net_test.go @@ -9,9 +9,10 @@ package originGoLanguage import ( "fmt" "net" + "testing" ) -func NetTest() { +func TestNet(t *testing.T) { //InterfaceAddrs 返回该系统的网络接口的地址列表。 addr, _ := net.InterfaceAddrs() diff --git a/example/originGoLanguage/OriginLangTest.go b/example/originGoLanguage/OriginLangTest.go deleted file mode 100644 index abcf9b5..0000000 --- a/example/originGoLanguage/OriginLangTest.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * @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() - -} diff --git a/example/originGoLanguage/QuoteTest.go b/example/originGoLanguage/Quote_test.go similarity index 91% rename from example/originGoLanguage/QuoteTest.go rename to example/originGoLanguage/Quote_test.go index 570a193..bd81469 100644 --- a/example/originGoLanguage/QuoteTest.go +++ b/example/originGoLanguage/Quote_test.go @@ -6,9 +6,12 @@ package originGoLanguage -import "fmt" +import ( + "fmt" + "testing" +) -func TestQuote() { +func TestQuote(t *testing.T) { x, y := "i am x", "i am y" fmt.Println("回参的值2") diff --git a/example/originGoLanguage/SliceTest.go b/example/originGoLanguage/Slice_test.go similarity index 91% rename from example/originGoLanguage/SliceTest.go rename to example/originGoLanguage/Slice_test.go index 051e856..ca9ca83 100644 --- a/example/originGoLanguage/SliceTest.go +++ b/example/originGoLanguage/Slice_test.go @@ -6,9 +6,12 @@ package originGoLanguage -import "fmt" +import ( + "fmt" + "testing" +) -func TestSlice() { +func TestSlice(t *testing.T) { //定义切片 var number []int fmt.Println(number) diff --git a/example/problem/MatrixRotation.go b/example/problem/MatrixRotation_test.go similarity index 95% rename from example/problem/MatrixRotation.go rename to example/problem/MatrixRotation_test.go index 17f6289..a8af241 100644 --- a/example/problem/MatrixRotation.go +++ b/example/problem/MatrixRotation_test.go @@ -6,13 +6,16 @@ package problem -import "fmt" +import ( + "fmt" + "testing" +) const ( SIZE = 4 ) -func MatrixRotationTest() { +func TestMatrixRotationTest(t *testing.T) { //矩阵旋转..... e := [SIZE][SIZE]int{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}} MatrixRotation(e) diff --git a/example/QrCodeExample.go b/example/qrCode/QrCode_test.go similarity index 94% rename from example/QrCodeExample.go rename to example/qrCode/QrCode_test.go index 30be9a5..036f5c9 100644 --- a/example/QrCodeExample.go +++ b/example/qrCode/QrCode_test.go @@ -7,6 +7,7 @@ package example import ( + "Go-StudyExample/example" "fmt" "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" @@ -15,6 +16,7 @@ import ( "image/png" "log" "os" + "testing" ) const ( @@ -26,7 +28,7 @@ const ( qrcode2 = "example/image/log_qrcode1.png" ) -func QrCodeTest() { +func TestQrCode(t *testing.T) { fmt.Printf("通过github.com/skips/go-qrcode 实现生成二维码") @@ -49,7 +51,7 @@ func QrCodeTest() { } else { qrCode1.BackgroundColor = color.RGBA{250, 250, 50, 255} //设置背景色 qrCode1.ForegroundColor = color.Black //设置二维码内背景色 - qrCode1.WriteFile(256, qrcode2) + _ = qrCode1.WriteFile(256, qrcode2) } //------------------------------------------------------------------------------------------------ @@ -58,20 +60,20 @@ func QrCodeTest() { content := "https://zhuanlan.zhihu.com/p/59125443" //二维码内容信息 code, err2 := qr.Encode(content, qr.L, qr.Unicode) //对二维码进行编码 - assert(err2) + example.Assert(err2) img, err3 := barcode.Scale(code, 300, 300) //图片大小设置 - assert(err3) + example.Assert(err3) file, err4 := os.Create("F:/Go_BySelf/src/Go-StudyExample/example/image/qr_code.png") //创建文件 - assert(err4) + example.Assert(err4) err5 := png.Encode(file, img) //图片编码生成 //err6 := jpeg.Encode(file, img, &jpeg.Options{100}) //图像质量值为100,是最好的图像显示 - assert(err5) + example.Assert(err5) err6 := file.Close() - assert(err6) + example.Assert(err6) //------------------------------------------------------------------------------------------------