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 package main
import ( import (
"Go-StudyExample/example"
"Go-StudyExample/example/originGoLanguage" "Go-StudyExample/example/originGoLanguage"
_ "Go-StudyExample/example/originGoLanguage/init" _ "Go-StudyExample/example/originGoLanguage/init"
"Go-StudyExample/example/problem"
) )
func main() { func main() {
//--------------------------测试mapStructure的功能-----
//这边的四种使用方式差别感觉不是很大...
example.MapStructureTestFunc()
example.MapStructureTestFunc1()
example.MapStructureTestFunc2()
example.MapStructureTestFunc3()
////--------------------------测试json包的转换功能
example.JsonMarshalTest()
example.JsonUnmarshalTest()
////克隆测试
example.TestClone()
////二维码测试
example.QrCodeTest()
//图片测试
example.ImageTest()
//go原生语言测试 //go原生语言测试
originGoLanguage.TestOriginLang() originGoLanguage.TestOriginLang()
//矩阵旋转
problem.MatrixRotationTest()
} }
+1 -1
View File
@@ -6,7 +6,7 @@
package example package example
func assert(err error) { func Assert(err error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
+4 -11
View File
@@ -18,13 +18,6 @@ import (
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
) )
func TestGetCpuMessage(t *testing.T) {
DiskCheck()
OSCheck()
CPUCheck()
RAMCheck()
}
const ( const (
B = 1 B = 1
KB = 1024 * B KB = 1024 * B
@@ -33,7 +26,7 @@ const (
) )
//服务器硬盘使用量 //服务器硬盘使用量
func DiskCheck() { func TestDiskCheck(t *testing.T) {
u, _ := disk.Usage("/") u, _ := disk.Usage("/")
usedMB := int(u.Used) / MB usedMB := int(u.Used) / MB
usedGB := int(u.Used) / GB usedGB := int(u.Used) / GB
@@ -44,12 +37,12 @@ func DiskCheck() {
} }
//OS //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()) fmt.Printf("goOs:%s,compiler:%s,numCpu:%d,version:%s,numGoroutine:%d\n", runtime.GOOS, runtime.Compiler, runtime.NumCPU(), runtime.Version(), runtime.NumGoroutine())
} }
//CPU 使用量 //CPU 使用量
func CPUCheck() { func TestCPUCheck(t *testing.T) {
cores, _ := cpu.Counts(false) cores, _ := cpu.Counts(false)
cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true) 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() u, _ := mem.VirtualMemory()
usedMB := int(u.Used) / MB usedMB := int(u.Used) / MB
totalMB := int(u.Total) / MB totalMB := int(u.Total) / MB
@@ -10,27 +10,16 @@ import (
"Go-StudyExample/entity" "Go-StudyExample/entity"
"Go-StudyExample/util" "Go-StudyExample/util"
"fmt" "fmt"
"testing"
) )
func TestClone() { func TestClone(t *testing.T) {
fmt.Print("\n\n\n")
fmt.Print(" Go语言深克隆的测试:\n\n")
testJustBasicType()
testHasPointType()
fmt.Println("由此可见其实Go语言对于内存的分配,相对对象是独一份,对于对象的基本类型和值类型(结构体),应该是在其内部开辟了内存空间进行存储,而对于" + fmt.Println("由此可见其实Go语言对于内存的分配,相对对象是独一份,对于对象的基本类型和值类型(结构体),应该是在其内部开辟了内存空间进行存储,而对于" +
"指针类型来说,直接使用它的地址(指针),不对结构体进行复制") "指针类型来说,直接使用它的地址(指针),不对结构体进行复制")
fmt.Println("")
testDeepCloneByEncode()
testDeepCloneByJsonPackage()
} }
func testDeepCloneByJsonPackage() { //json包处理深度克隆
func TestDeepCloneByJsonPackage(t *testing.T) {
rule := entity.SynthesisRule{ rule := entity.SynthesisRule{
SynthesisId: "112", SynthesisId: "112",
SynthesisNum: 0, SynthesisNum: 0,
@@ -45,7 +34,8 @@ func testDeepCloneByJsonPackage() {
} }
} }
func testDeepCloneByEncode() { //序列化方式进行深度克隆
func TestDeepCloneByEncode(t *testing.T) {
rule := entity.SynthesisRule{ rule := entity.SynthesisRule{
SynthesisId: "112", SynthesisId: "112",
SynthesisNum: 0, SynthesisNum: 0,
@@ -63,7 +53,8 @@ func testDeepCloneByEncode() {
} }
} }
func testJustBasicType() { //结构体带基本类型
func TestJustBasicType(t *testing.T) {
rule := entity.SynthesisRule{ rule := entity.SynthesisRule{
SynthesisId: "112", SynthesisId: "112",
SynthesisNum: 0, SynthesisNum: 0,
@@ -83,7 +74,8 @@ func testJustBasicType() {
} }
func testHasPointType() { //结构体带指针
func TestHasPointType(t *testing.T) {
cmp := entity.SynthesisRuleCmp{ cmp := entity.SynthesisRuleCmp{
SynthesisRule: &entity.SynthesisRule{ SynthesisRule: &entity.SynthesisRule{
SynthesisId: "112", SynthesisId: "112",
@@ -104,7 +96,8 @@ func testHasPointType() {
} }
} }
func testHasPointTypeList() { //结构体带切片
func TestHasPointTypeSlice(t *testing.T) {
cmp := entity.SynthesisRuleCmp{ cmp := entity.SynthesisRuleCmp{
SynthesisRule: &entity.SynthesisRule{ SynthesisRule: &entity.SynthesisRule{
@@ -18,6 +18,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"testing"
) )
const ( const (
@@ -33,7 +34,7 @@ const (
wordPath = "example/image/wordPath.png" //携带文字的图片 wordPath = "example/image/wordPath.png" //携带文字的图片
) )
func ImageTest() { func TestImage(t *testing.T) {
AddWordToImg() //设置文字到图片 AddWordToImg() //设置文字到图片
@@ -4,18 +4,19 @@
* @Description * @Description
*/ */
package example package jsonPackage
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"testing"
"time" "time"
) )
type Time time.Time type Time time.Time
const ( 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{ j := json1{
Name: "name", Name: "name",
Value: "value", 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"}` src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}`
p := new(Person) p := new(Person)
err := json.Unmarshal([]byte(src), &p) err := json.Unmarshal([]byte(src), &p)
@@ -89,7 +90,7 @@ func JsonUnmarshalTest() {
//实现该方法,实现对应的时间处理,json应该是没有支持时间处理的 //实现该方法,实现对应的时间处理,json应该是没有支持时间处理的
func (t *Time) UnmarshalJSON(data []byte) (err error) { 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) *t = Time(now)
return return
} }
@@ -98,13 +99,13 @@ func (t *Time) UnmarshalJSON(data []byte) (err error) {
* 实现该方法确定时间格式的输出 * 实现该方法确定时间格式的输出
*/ */
func (t Time) MarshalJSON() ([]byte, 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 = append(b, '"')
b = time.Time(t).AppendFormat(b, timeFormart) b = time.Time(t).AppendFormat(b, timeFormat)
b = append(b, '"') b = append(b, '"')
return b, nil return b, nil
} }
func (t Time) String() string { func (t Time) String() string {
return time.Time(t).Format(timeFormart) return time.Time(t).Format(timeFormat)
} }
@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/goinggo/mapstructure" "github.com/goinggo/mapstructure"
"testing"
) )
//-----------------------json数据--------------------- //-----------------------json数据---------------------
@@ -31,7 +32,7 @@ type Items struct {
//---------------------------------测试方法------------------------ //---------------------------------测试方法------------------------
func MapStructureTestFunc() { func TestMapStructureTestFunc(t *testing.T) {
var te entity2.Entity var te entity2.Entity
m := make(map[string]interface{}) m := make(map[string]interface{})
m["Num"] = 1 m["Num"] = 1
@@ -46,7 +47,7 @@ func MapStructureTestFunc() {
fmt.Print(te.Num, " ", te.S, " ", te.T) fmt.Print(te.Num, " ", te.S, " ", te.T)
} }
func MapStructureTestFunc1() { func TestMapStructureTestFunc1(t *testing.T) {
var docMap map[string]interface{} var docMap map[string]interface{}
_ = json.Unmarshal([]byte(document), &docMap) _ = json.Unmarshal([]byte(document), &docMap)
@@ -64,7 +65,7 @@ type NameDoc struct {
Name string `jpath:"name"` Name string `jpath:"name"`
} }
func MapStructureTestFunc2() { func TestMapStructureTestFunc2(t *testing.T) {
sliceScript := []byte(document2) sliceScript := []byte(document2)
var sliceMap []map[string]interface{} var sliceMap []map[string]interface{}
@@ -80,7 +81,7 @@ func MapStructureTestFunc2() {
fmt.Println(myslice[0], " ", myslice[1]) fmt.Println(myslice[0], " ", myslice[1])
} }
func MapStructureTestFunc3() { func TestMapStructureTestFunc3(t *testing.T) {
docScript := []byte(document1) docScript := []byte(document1)
var docMap map[string]interface{} var docMap map[string]interface{}
_ = json.Unmarshal(docScript, &docMap) _ = json.Unmarshal(docScript, &docMap)
@@ -6,9 +6,12 @@
package originGoLanguage package originGoLanguage
import "fmt" import (
"fmt"
"testing"
)
func TestChannel() { func TestChannel(t *testing.T) {
list := []int{0, 1, 45, -12, 33, 90, -22, 100} list := []int{0, 1, 45, -12, 33, 90, -22, 100}
//通道的初始化 //通道的初始化
@@ -8,10 +8,11 @@ package originGoLanguage
import ( import (
"fmt" "fmt"
"testing"
"time" "time"
) )
func TestDefer() { func TestDefer(t *testing.T) {
//defer会在所有函数执行完成之后才进行执行 //defer会在所有函数执行完成之后才进行执行
testDeferExe() testDeferExe()
//defer产生的值是在对应位置的值,后面的变化不会产生影响 //defer产生的值是在对应位置的值,后面的变化不会产生影响
@@ -22,7 +23,7 @@ func TestDefer() {
testDeferRecoverOut() //外层异常捕获 testDeferRecoverOut() //外层异常捕获
} }
func TestCtripException() { func TestCtripException(t *testing.T) {
fmt.Println("测试不进行捕获的情况") fmt.Println("测试不进行捕获的情况")
testNotDefer() testNotDefer()
@@ -9,9 +9,10 @@ package originGoLanguage
import ( import (
"errors" "errors"
"fmt" "fmt"
"testing"
) )
func TestError() { func TestError(t *testing.T) {
fmt.Println("测试创建一个新的错误") fmt.Println("测试创建一个新的错误")
testNewError() testNewError()
@@ -6,9 +6,12 @@
package originGoLanguage package originGoLanguage
import "fmt" import (
"fmt"
"testing"
)
func TestMap() { func TestMap(t *testing.T) {
//没有初始化的是nil,不能进行赋值 //没有初始化的是nil,不能进行赋值
var countryMap1 map[string]string var countryMap1 map[string]string
fmt.Println(countryMap1 == nil) fmt.Println(countryMap1 == nil)
@@ -9,9 +9,10 @@ package originGoLanguage
import ( import (
"fmt" "fmt"
"net" "net"
"testing"
) )
func NetTest() { func TestNet(t *testing.T) {
//InterfaceAddrs 返回该系统的网络接口的地址列表。 //InterfaceAddrs 返回该系统的网络接口的地址列表。
addr, _ := net.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 package originGoLanguage
import "fmt" import (
"fmt"
"testing"
)
func TestQuote() { func TestQuote(t *testing.T) {
x, y := "i am x", "i am y" x, y := "i am x", "i am y"
fmt.Println("回参的值2") fmt.Println("回参的值2")
@@ -6,9 +6,12 @@
package originGoLanguage package originGoLanguage
import "fmt" import (
"fmt"
"testing"
)
func TestSlice() { func TestSlice(t *testing.T) {
//定义切片 //定义切片
var number []int var number []int
fmt.Println(number) fmt.Println(number)
@@ -6,13 +6,16 @@
package problem package problem
import "fmt" import (
"fmt"
"testing"
)
const ( const (
SIZE = 4 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}} e := [SIZE][SIZE]int{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}
MatrixRotation(e) MatrixRotation(e)
@@ -7,6 +7,7 @@
package example package example
import ( import (
"Go-StudyExample/example"
"fmt" "fmt"
"github.com/boombuler/barcode" "github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr" "github.com/boombuler/barcode/qr"
@@ -15,6 +16,7 @@ import (
"image/png" "image/png"
"log" "log"
"os" "os"
"testing"
) )
const ( const (
@@ -26,7 +28,7 @@ const (
qrcode2 = "example/image/log_qrcode1.png" qrcode2 = "example/image/log_qrcode1.png"
) )
func QrCodeTest() { func TestQrCode(t *testing.T) {
fmt.Printf("通过github.com/skips/go-qrcode 实现生成二维码") fmt.Printf("通过github.com/skips/go-qrcode 实现生成二维码")
@@ -49,7 +51,7 @@ func QrCodeTest() {
} else { } else {
qrCode1.BackgroundColor = color.RGBA{250, 250, 50, 255} //设置背景色 qrCode1.BackgroundColor = color.RGBA{250, 250, 50, 255} //设置背景色
qrCode1.ForegroundColor = color.Black //设置二维码内背景色 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" //二维码内容信息 content := "https://zhuanlan.zhihu.com/p/59125443" //二维码内容信息
code, err2 := qr.Encode(content, qr.L, qr.Unicode) //对二维码进行编码 code, err2 := qr.Encode(content, qr.L, qr.Unicode) //对二维码进行编码
assert(err2) example.Assert(err2)
img, err3 := barcode.Scale(code, 300, 300) //图片大小设置 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") //创建文件 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) //图片编码生成 err5 := png.Encode(file, img) //图片编码生成
//err6 := jpeg.Encode(file, img, &jpeg.Options{100}) //图像质量值为100,是最好的图像显示 //err6 := jpeg.Encode(file, img, &jpeg.Options{100}) //图像质量值为100,是最好的图像显示
assert(err5) example.Assert(err5)
err6 := file.Close() err6 := file.Close()
assert(err6) example.Assert(err6)
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------