- 博客(19)
- 收藏
- 关注
原创 go net/http实现路由函数转换的方法
package mainimport "fmt"type MHandler interface { ServeMyHTTP(req string, res string)}type MHandlerFunc func(req string, res string)func (f MHandlerFunc) ServeMyHTTP(req string, res string) { f(req, res)}func main() { h := func(req string.
2021-12-14 20:01:38
2060
原创 简单背包问题
package mainimport ( "fmt" "sort")type Item struct { Weight int Value int Ratio float64}func NewItem(weight int, value int) Item { item := Item { Weight: weight, Value: value, } item.Ratio = float64(value) / float64(weight) return ite.
2021-10-25 12:20:34
94
原创 mysql开启慢查询
查看是否开启慢查询show variables like 'slow_query_log%';show variables like 'long_query_time%';永久生效 修改`/etc/my.cnf`文件,添加一下配置,然后重启服务[mysqld]slow_query_log=1slow_query_log_file=/var/lib/mysql/slow-log.loglong_query_time=1...
2021-09-07 11:06:26
84
原创 git rebase 合并多个commit
当我们在某个分支上提交了多次`commit`的时候,如果直接推送到远程分支会污染提交历史。这时我们可以把多次`commit`合并成一次。 首先创建三个文件,每次都执行`git add .`和`git commit -m "xxx"` 命令。然后执行`git log`查看提交历史。touch 1.txtgit add .git commit -m "add 1"touch 2.txtgit add .git commit -m "add 2"touch 3....
2021-08-25 15:40:28
255
原创 go接口型函数
如何将一个函数(函数签名与F一致)转化为接口A: 定义一个函数类型 F,并且实现接口 A 的方法,然后在这个方法中调用自己。package mainimport "fmt"type A interface { Hello(k string, v string)}func B(k string, v string) { fmt.Println("B: ", k, v)}type C func(k string, v string)func (c C) Hel...
2021-08-12 15:05:48
191
原创 go web服务优雅关闭
package mainimport ( "context" "log" "net/http" "os" "os/signal" "time")func hello(w http.ResponseWriter, r *http.Request) { time.Sleep(20 * time.Second) w.Write([]byte("hello-v1")) log.Println("v1", r.RemoteAddr)}func main() { http.Han.
2021-07-26 03:13:09
365
原创 golang rsa签名与验签
package mainimport ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/hex" "encoding/pem" "errors" "fmt")const ( publicKey = `-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyPWejY7A+stkupI5Ow1aq.
2021-07-21 22:50:47
540
原创 golang Functional Options Pattern
type options struct { PanicWhenInitFail bool Debug bool}type optionFunc func(*options)func WithPanicWhenInitFail() optionFunc { return func(o *options) { o.PanicWhenInitFail = true }}func WithDebug() optionFunc { return func(o *.
2021-07-15 23:39:10
151
原创 go http中间件实现
```type DecoratorHandler func(http.HandlerFunc) http.HandlerFuncfunc MiddlewareHandlerFunc(hp http.HandlerFunc, decors ...DecoratorHandler) http.HandlerFunc { for d := range decors { dp := decors[len(decors)-1-d] hp = dp(hp) } ret
2021-07-13 18:15:41
204
原创 golang实现中间件
```type MiddlewareFunc func(HandlerFunc) HandlerFunctype HandlerFunc func() errorfunc applyMiddleware(h HandlerFunc, middleware MiddlewareFunc) HandlerFunc { h = middleware(h) return h}func main() { h := func() error { time.Sleep(
2021-07-07 16:06:34
287
原创 同步时间服务
centos定时同步时间服务器```*/5 * * * * root /usr/sbin/ntpdate ntp1.aliyun.com```在`/etc/cron.d`目录下创建文件`ntp.cron`然后写入以上内容即可,每五分钟执行一遍同步服务.
2021-03-05 10:25:13
140
1
原创 linux获取本机ipaddress或hostname
获取iphostname -I | cut -d" " -f 1获取hostnamehostname -f
2020-05-08 16:39:06
723
原创 react hooks demo
先上个demo看下import React, { useState } from "react";function Example() { const [count, setCount] = useState(0); return ( <> Count: {count} <button onClick={() =&g...
2020-04-29 16:03:58
147
原创 快递鸟查询demo
快递鸟查询demoimport jsonfrom pprint import pprintimport requestsimport hashlibfrom base64 import b64encodeclass KdNiaoAPI: """快递鸟查询接口""" def __init__(self): # ID和Key请到官网申请:http://www....
2019-07-05 17:31:33
646
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人