feat(Go-StudyExample):包方法重构

This commit is contained in:
Huangzj
2020-12-16 11:30:31 +08:00
parent 1d21a9634e
commit a97c2c9bcc
17 changed files with 74 additions and 125 deletions
-23
View File
@@ -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()
}
+1 -1
View File
@@ -6,7 +6,7 @@
package example
func assert(err error) {
func Assert(err error) {
if err != nil {
panic(err)
}
+4 -11
View File
@@ -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
@@ -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{
@@ -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() //设置文字到图片
@@ -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)
}
@@ -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)
@@ -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}
//通道的初始化
@@ -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()
@@ -9,9 +9,10 @@ package originGoLanguage
import (
"errors"
"fmt"
"testing"
)
func TestError() {
func TestError(t *testing.T) {
fmt.Println("测试创建一个新的错误")
testNewError()
@@ -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)
@@ -9,9 +9,10 @@ package originGoLanguage
import (
"fmt"
"net"
"testing"
)
func NetTest() {
func TestNet(t *testing.T) {
//InterfaceAddrs 返回该系统的网络接口的地址列表。
addr, _ := net.InterfaceAddrs()
@@ -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()
}
@@ -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")
@@ -6,9 +6,12 @@
package originGoLanguage
import "fmt"
import (
"fmt"
"testing"
)
func TestSlice() {
func TestSlice(t *testing.T) {
//定义切片
var number []int
fmt.Println(number)
@@ -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)
@@ -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)
//------------------------------------------------------------------------------------------------