![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
GO语言
鸭鸭老板
偏执只会左右你的思考
展开
-
go学习 --- panic函数
一、panic函数package mainimport "fmt"//定义函数Afunc TestA() { fmt.Println("错误一")}//定义函数Bfunc TestB() { //程序崩溃,实际开发中少用 panic("错误二")}//定义函数Cfunc TestC() { fmt.Println("错误三")}func main() { TestA() TestB() TestC()}...原创 2022-02-26 13:31:17 · 680 阅读 · 0 评论 -
go学习 --- 使用接口泛型定位错误
一、使用错误接口泛型定位错误类型package mainimport "fmt"//解析错误type ParseError struct { FileName string //文件名 Line int //行号}//实现error接口,错误描述func (e *ParseError) Error() string { return fmt.Sprintf("%s:%d",e.FileName,e.Line)}//创建错误解析func newError(fileName原创 2022-02-25 20:12:04 · 194 阅读 · 0 评论 -
go学习 --- 实现约瑟夫环
一、实现约瑟夫环1、方式一package mainimport ( "container/ring" "fmt")type Player struct { pos int aLive bool}const ( playerCount = 100 startPos = 1 deadline = 3)func main() { r := ring.New(playerCount) //循环加载 for i := startPos; i <=playerC原创 2022-02-24 21:06:50 · 313 阅读 · 0 评论 -
go学习 --- 环形链表
一、环形链表package mainimport ( "container/ring" "fmt")func main() { r := ring.New(5) for i := 0; i < 5; i++ { r.Value = i r = r.Next() //循环下一个 } r.Do(func(i interface{}) { fmt.Println(i) }) r3 := r.Move(3) //删除第三个元素 fmt.Println(r3) f原创 2022-02-23 22:12:41 · 401 阅读 · 0 评论 -
go学习 --- 链表与切片性能测试
一、链表与切片性能测试package mainimport ( "container/list" "fmt" "time")//测试链表与切片性能func T1() { //开始时间 start := time.Now() slice := make([]int, 1) //循环添加数据 for i := 0; i < 10000000; i++ { slice = append(slice, 1) } //结束时间 fmt.Println("切片的添加数据原创 2022-02-22 21:12:32 · 441 阅读 · 0 评论 -
go学习 --- 链表的实现
一、链表的实现package mainimport ( "container/list" "fmt")func main() { List := list.New() for i := 0; i < 20; i++ { List.PushFront(i) } for it := List.Front(); it != nil ; it = it.Next() { fmt.Println(it.Value) fmt.Println(it.Prev())//前一个原创 2022-02-21 20:02:17 · 391 阅读 · 0 评论 -
go学习 --- 堆的实现
一、堆的实现package mainimport ( "container/heap" "fmt" "math/rand" "time")type Girl struct { name string age int tall int beauty int}type GirlHeap []Girl//比大小func (h *GirlHeap) Less(i, j int) bool { //return (*h)[i].age > (*h)[j].age原创 2022-02-20 16:40:23 · 334 阅读 · 0 评论 -
go学习 --- 并发测试
一、并发测试package concurrentTestimport ( "bytes" "html/template" "testing")//BenchmarkParallel 并发批量测试func BenchmarkParallel(b *testing.B) { must := template.Must(template.New("test").Parse("你好,{{.}}")) //并发执行 b.RunParallel(func(pb *testing.PB)原创 2022-02-19 16:02:41 · 1011 阅读 · 0 评论 -
go学习 --- web测试
一、web测试package webTestimport ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "testing")func helloHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world"))}//测试webfunc TestConn(t *testing.T) { recorder := httpte原创 2022-02-18 21:40:27 · 286 阅读 · 0 评论 -
go学习 --- TestMain测试
一、TestMain测试package TestMainimport ( "fmt" "os" "testing")func setup() { fmt.Println("setup all tests")}func tearDown() { fmt.Println("tearDown all tests")}func Test1(t *testing.T) { fmt.Println("i'm test1")}func Test2(t *testing.T)原创 2022-02-17 22:34:03 · 1526 阅读 · 2 评论 -
go学习 --- 单元测试子测试
一、单元测试子测试package subtestimport "testing"//子测试func TestSum(t *testing.T) { t.Run("ping", func(t *testing.T) { if Sum(2,5) != 10 { t.Fatal("fail") } }) t.Run("dog", func(t *testing.T) { if Sum(2,-5) != -10 { t.Fatal("fail") } })}/原创 2022-02-15 21:30:09 · 350 阅读 · 0 评论 -
go学习 --- 读取ini文件
一、读取ini文件1、iniconfig工具类package iniConfigimport ( "errors" "fmt" "io/ioutil" "reflect" "strconv" "strings")func MarshalFile(filename string, data interface{}) (err error) { result, err := Marshal(data) if err != nil { return } return原创 2022-02-13 22:46:23 · 1221 阅读 · 0 评论 -
go学习 --- 依赖注入
一、依赖注入package mainimport "fmt"func a1() { fmt.Println("a1调用")}func a2() { fmt.Println("a2调用")}func main() { //通过map实现依赖注入 myMap := make(map[string]func()) myMap["a1"] = a1 myMap["a2"] = a2 //通过map的key调用函数 myMap["a1"]() myMap["a2"]()}原创 2022-02-13 22:00:17 · 991 阅读 · 0 评论 -
go学习 --- 结构体转map[string]interface{}
一、结构体转map[string]interface{}的类型问题package mainimport ( "encoding/json" "fmt")//定义一个结构体type User struct { Name string `json:"name"` Age int `json:"age"`}func main() { user := User{ Name: "小王", Age: 25, } m, _ := json.Marshal(&user)原创 2022-02-13 21:08:48 · 1328 阅读 · 0 评论 -
go学习 --- gorm框架删除数据
一、使用gorm删除数据package mainimport ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql")//创建映射结构体type User struct { //gorm.Model框架自带的结构体 gorm.Model Name string Age int64}func main() { //连接数据库 db, err := gorm.Open("my原创 2022-02-11 18:20:33 · 2234 阅读 · 0 评论 -
go学习 --- 使用gorm修改数据表
一、使用gorm框架修改mysql数据package mainimport ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql")//创建映射结构体type User struct { //gorm.Model框架自带的结构体 gorm.Model Name string Age int64}func main() { //连接数据库 db, err := gorm.O原创 2022-02-11 17:40:20 · 2183 阅读 · 0 评论 -
go学习 --- gorm查询
一、条件查询package mainimport ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql")//创建映射结构体type User struct { //gorm.Model框架自带的结构体 gorm.Model Name string Age int64}func main() { //连接数据库 db, err := gorm.Open("mysql",原创 2022-02-11 14:23:26 · 2268 阅读 · 0 评论 -
go学习 --- 使用sqlx操作数据库
一、安装sqlxpackage mainimport ( "fmt" _ "github.com/go-sql-driver/mysql" //初始化 "github.com/jmoiron/sqlx")//创建与数据库映射的结构体type user struct { Id int Name string Age int}//创建一个全局对象var pool *sqlx.DBfunc InItDB() (err error) { dsn := "root:1.原创 2022-02-10 00:01:04 · 563 阅读 · 0 评论 -
go学习 --- go对mysql的增删改查
一、go实现对数据库的增删改查package mainimport ( "database/sql" "fmt" _"github.com/go-sql-driver/mysql" //初始化)//定义一个全局变量var db *sql.DB//定义初始化数据库的函数func initDB()(err error) { dsn := "root:123456@tcp(127.0.0.1:3306)/db02?charset=utf8mb4&parseTime=True原创 2022-02-09 21:41:32 · 981 阅读 · 0 评论 -
go学习 --- 连接mysql数据库
一、连接mysql数据库1、下载第三方库package mainimport ( "database/sql" "fmt" _"github.com/go-sql-driver/mysql" //初始化)func main() { dsn := "root:123456@tcp(127.0.0.1:3306)/db02" //打开数据库链接 db, err := sql.Open("mysql", dsn) if err != nil { fmt.Println("原创 2022-02-09 19:47:01 · 5188 阅读 · 0 评论 -
go学习 --- godoc工具安装
一、godoc工具安装二、配置环境变量原创 2022-02-09 17:04:38 · 735 阅读 · 0 评论 -
go学习 --- go运行Windows指令
一、go运行Windows指令package mainimport "os/exec"func main() { //要执行的Windows的指令 command := exec.Command("notepad") command.Run() //执行}原创 2022-02-09 15:46:38 · 2215 阅读 · 1 评论 -
go学习 --- iris日志管理
一、iris日志管理1、运行日志系统2、访问系统3、生成日志原创 2022-02-07 16:01:14 · 780 阅读 · 0 评论 -
go学习 --- iris框架源码下载运行
一、下载iris源码二、初始化框架三、测试框架原创 2022-02-07 15:35:48 · 269 阅读 · 0 评论 -
go学习 ---- 安装测试iris框架
一、iris官网iris/README_ZH.md at master · kataras/iris · GitHub二、安装iris三、测试框架package mainimport ( "github.com/kataras/iris/v12")func main() { app := iris.New() booksAPI := app.Party("/books") { booksAPI.Use(iris.Compression) // GET: .原创 2022-02-07 10:56:45 · 581 阅读 · 0 评论 -
go 学习--- rpc远程调用
一、rpc1、server.gopackage mainimport ( "net/rpc" "net/http" "log")type Params struct { Width, Height int}type Rect struct{}func (r *Rect) Area(p Params, ret *int) error { *ret = p.Width * p.Height return nil}func (r *Rect) Perimeter(p原创 2022-02-06 16:52:27 · 308 阅读 · 0 评论 -
go学习 --- 解析和生成xml文件
一、解析xmlpackage mainimport ( "encoding/xml" "fmt" "io/ioutil" "os")type Recurlyservers struct { XMLName xml.Name `xml:"servers"` Version string `xml:"version,attr"` Svs []server `xml:"server"` //整个xml文本内容 Description string `xml:",innerxml"原创 2022-02-06 16:13:06 · 1289 阅读 · 0 评论 -
go学习 --- web的session会话
一、session会话1、session.gopackage mainimport ( "crypto/rand" "encoding/base64" "fmt" "io" "net/http" "net/url" "sync" "time")type Session interface { Set(key, value interface{}) error //set session value Get(key interface{}) interface{} /原创 2022-02-06 11:09:45 · 231 阅读 · 0 评论 -
go学习 --- web生成token避免重复提交
一、token避免反复提交package mainimport ( "crypto/md5" "fmt" "html/template" "io" "net/http" "strconv" "time")func login(w http.ResponseWriter, r *http.Request) { //获取请求方法 fmt.Println("method:",r.Method) //判断是否是get请求方法 if r.Method == "GET" {原创 2022-02-05 13:50:28 · 472 阅读 · 0 评论 -
go学习 --- web校验form信息校验
一、form输入校验package mainimport ( "fmt" "html/template" "log" "net/http" "regexp" "strconv")func register(w http.ResponseWriter,r *http.Request) { r.ParseForm() //验证必填字段 username := r.Form.Get("username") if len(username) == 0 { fmt.Prin原创 2022-02-05 11:40:16 · 286 阅读 · 0 评论 -
go学习 --- web的form截取登陆信息
一、form截取登陆信息package mainimport ( "fmt" "html/template" "log" "net/http" "net/url" "strings")//定义一个函数func Hello(w http.ResponseWriter,r *http.Request) { //解析url传递的参数,post请求则解析响应包的主体 r.ParseForm() //获取表单数据 fmt.Println(r.Form) //获取url的路径原创 2022-02-04 16:18:07 · 346 阅读 · 0 评论 -
go学习 --- web检测重定向
一、检测重定向package mainimport ( "fmt" "net/http" "net/http/httputil")func main() { request, err := http.NewRequest(http.MethodGet, "http://www.baidu.com", nil) if err != nil { panic(err.Error()) } request.Header.Add("User-Agent","Mozilla/5.0(i原创 2022-02-04 15:19:21 · 211 阅读 · 0 评论 -
go学习 --- web的cookie信息
一、serverpackage mainimport ( "fmt" "io" "io/ioutil" "log" "net/http")func main() { http.HandleFunc("/login",login) err := http.ListenAndServe(":2001", nil) if err != nil { log.Fatal(err) }}func login(w http.ResponseWriter,r *http.Requ原创 2022-02-04 15:02:09 · 366 阅读 · 0 评论 -
go学习 --- web的post数据
一、post1、serverpackage mainimport ( "fmt" "io" "io/ioutil" "log" "net/http")func main() { //创建路由 server := http.NewServeMux() server.HandleFunc("/login",login) http.ListenAndServe(":2001",server)}func login(w http.ResponseWriter,r *htt原创 2022-02-02 11:13:55 · 313 阅读 · 0 评论 -
go学习 --- web的response返回消息
一、response返回消息package mainimport ( "fmt" "io/ioutil" "log" "net/http")func main() { str := "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京" resp, err := http.Get(str) if err != nil { log.Fat原创 2022-01-31 15:38:26 · 695 阅读 · 0 评论 -
go学习 --- web读取网页的内容
一、读取网页内容package mainimport ( "fmt" "io/ioutil" "net/http")func main() { url := "http://www.baidu.com" //将要读取的网站放入到get方法中 resp, err := http.Get(url) if err != nil { panic(err.Error()) } //读取数据 bytes, err := ioutil.ReadAll(resp.Body) if原创 2022-01-31 14:41:35 · 698 阅读 · 0 评论 -
go学习 --- web冒充手机访问网页
一、冒充手机访问网页package mainimport ( "fmt" "net/http" "net/http/httputil")func main() { request, err := http.NewRequest(http.MethodGet, "http://www.baidu.com", nil) if err != nil { panic(err.Error()) } request.Header.Add("User-Agent","Mozilla/5.原创 2022-01-31 14:18:53 · 332 阅读 · 0 评论 -
go学习 --- web实现中间件功能
一、中间件的实现package mainimport ( "fmt" "net/http")//定义一个结构体type HelloHandler struct {}//给结构体绑定一个方法func (hh *HelloHandler) ServeHTTP(w http.ResponseWriter,r *http.Request) { fmt.Fprintln(w,"你好呀!")}//定义一个函数.实现中间件功能func logHandler(h http.Handler原创 2022-01-31 13:48:05 · 203 阅读 · 0 评论 -
go学习 ---- web的url-route
一、url-route下载第三方包:go get github.com/julienschmidt/httprouterpackage mainimport ( "fmt" "github.com/julienschmidt/httprouter" "net/http")func hello(w http.ResponseWriter,r *http.Request,p httprouter.Params) { fmt.Fprintf(w,"Hello%s!\n",原创 2022-01-31 13:21:51 · 178 阅读 · 0 评论 -
go学习 --- web文件上传
一、http文件下载1、upload.gtpl<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"/> <title>Upload File </title></head><body> <form action="/upload?hello=world&thread=123" method="post" e原创 2022-01-31 12:32:00 · 448 阅读 · 0 评论