Giter Site home page Giter Site logo

yulu-au.github.io's Introduction

issue

yulu-au.github.io's People

Contributors

yulu-au avatar

Watchers

 avatar

yulu-au.github.io's Issues

MySQL 的 COLLATE

是什么

它是字符类型的排序规则,通常写建表语句的时候会用到,与charset紧密相关.
比如你选择了utfmb4作为字符集那么紧接着就要选择哪种排序规则

怎么做

目前通常选择utf8mb4+utf8mb4_0900_ai_ci作为字符集跟排序规则,前者的奇葩命名来自于mysql的历史问题,后者是说大小写不敏感(Case Insensitive)

ps

排序规则的后缀大致三种
cs Case Sensitive
ci Case Insensitive
bin binary 将所有字符看作二进制串

初探 github.com/pkg/errors

why

golang原生的error处理十分简单,没有跟踪函数栈的信息

what

golang第二版的草案提到了github.com/pkg/errors这种机制解决了two main problems—error inspection and error formatting
这个包里暴露出来的函数有
errors

其中主要的New和Wrap原理示意如下
errors

how

①要抛出一个error的时候而又没有父级别(意会一下)的error时,通常使用New方法或者Errorf初始化结构体fundamental,它本身包含了提示信息message和函数栈跟踪信息stack
②当有父级别的error时,使用Wrap来包装一下,不仅携带父级别的error,而且还可以携带这一次的提示信息message和函数栈跟踪stack
③Wrap可以多次包装,那就会形成一条链,正因为如此保留了最大程度的错误信息

总结

现在传递error不仅能跟踪函数栈,而且还能判定类型(IS函数可以判断某个error是不是在链上)

Handler/HandlerFunc傻傻分不清楚之设计模式

背景

golang的http包围绕着ServeHTTP方法展开,实现了这个方法就能够处理http连接的业务逻辑,然而却有两种注册处理函数的方案,二者的作用是一致的,这部分代码是明显的适配器模式

//http包的核心接口,业务逻辑就写在这里
type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}


//适配器模式的实现
type HandlerFunc func(ResponseWriter, *Request)
// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
	f(w, r)
}

func (mux *ServeMux) Handle(pattern string, handler Handler)

它的作用就是上文讲的注册业务逻辑
传递实现了Handler接口的handler,http请求进来的时候 handler.ServeHttp就能为http请求做业务逻辑处理

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))

也是注册业务逻辑
传递一个函数,但是它的功能又能跟上边一致,那很明显 handler.ServeHttp 也是一定要满足的
该函数做了什么呢

{handler 它的类型是 func(ResponseWriter, *Request)} 转换为 {h 它的类型是HandlerFunc}

handler原本是不能调用ServeHttp方法的,它自己就是个方法而不是对象,但是转换为类型HandlerFunc的 “h” 之后,它就有了自己的方法ServeHttp,注意看这个ServeHttp方法还是有点不一样,它调用自身也就是handler

总结

这种方案可以让函数伪装成对象,以后碰到那种声明struct而又觉着好多余、好难看的情况,这种代码结构说不定就可以用上了

记一次go升级 关键词 GOFLAGS ="-mod=mod"

背景

go从1.13升级到1.16

编译问题

编译
在专门编译的机器记为 X 上编译报错,但是在自己的开发机上没问题

报错

go: github.com/axgle/[email protected]: 
missing go.sum entry; to add it:
go mod download github.com/axgle/mahonia

解决过程

比较开发机上git仓库与编译机的区别,发现是没有go.sum这个文件导致编译的时候报错,.gitignore里面有go.sum

搜索
google一下关键词,发现有解决方案,
export GOFLAGS ="-mod=mod"
成功

深究问题原因

问题是没有产生go.sum这个文件导致的,为什么1.13 build 的时候 能自动产生而1.16不能了呢

搜索

GOFLAGS很少被使用,google一下发现没有多少文章介绍,也没有关于
-mod=mod 的解释

搜索

直接去golang官方的文档里查找,GOFLAGS是go env打印出来的所以
定位文档
https://golang.org/cmd/go/#hdr-Environment_variables
定位 Environment variables
定位 GOFLAGS

GOFLAGS
	A space-separated list of -flag=value settings to apply
	to go commands by default, when the given flag is known by
	the current command. Each entry must be a standalone flag.
	Because the entries are space-separated, flag values must
	not contain spaces. Flags listed on the command line
	are applied after this list and therefore override it.

意思就是设置当前命令的默认动作
当前命令是build,那么build的默认参数里应该有 -mod=mod
go help build |grep mod 果然有

 -mod mode
 module download mode to use: readonly, vendor, or mod.
 By default, if a vendor directory is present and the go version in go.mod
 is 1.14 or higher, the go command acts as if -mod=vendor were set.
 Otherwise, the go command acts as if -mod=readonly were set.
 See https://golang.org/ref/mod#build-commands for details.

找到目的文档

one piece

https://golang.org/ref/mod#build-commands


The -mod flag controls whether go.mod may be automatically updated and whether the vendor directory is used.
	 -mod=mod tells the go command to ignore the vendor directory and to automatically update go.mod, for example, when an imported package is not provided by any known module.
	 -mod=readonly tells the go command to ignore the vendor directory and to report an error if go.mod needs to be updated.
	 -mod=vendor tells the go command to use the vendor directory. In this mode, the go command will not use the network or the module cache.
	 By default, if the go version in go.mod is 1.14 or higher and a vendor directory is present, the go command acts as if -mod=vendor were used. Otherwise, the go command acts as if -mod=readonly were used.

意思是说go build 命令加这个参数相当于go mod tidy + go build
这里超链接了另一个文档
https://golang.org/ref/mod#go-mod-file-updates
找到问题的根本原因

In Go 1.15 and lower, the -mod=mod flag was enabled by default, so updates were performed automatically. 
Since Go 1.16, the go command acts as if -mod=readonly were set instead: 
if any changes to go.mod are needed, the go command reports an error and suggests a fix.

在1.15之前 mod 是默认加上的,1.16时默认的是readonly,就是go build 之前再也不隐式执行go mod tidy了,而我的git仓库里面go.sum是忽略的,X上面没有go.sum导致无法编译完成
github.com/axgle/[email protected]恰巧是go.mod的第一行

总结

1.16之后的版本,编译的时候不会隐式执行go mod tidy这种自动更新依赖关系的动作,要想一切如常就得设置环境变量 export GOFLAGS ="-mod=mod"

Docker的bridge模式

前置知识

①安装docker之后,宿主机ifconfig可以看到多了一块网卡docker0 [inet 172.17.0.1 netmask 255.255.0.0],
②容器内部与docker0相连的网卡 eth0

图片描述

docker_bridge

自定义网络模式

可以使用docker network create xxxx 自定义一块网卡来桥接容器,相当于代替了docker0的gateway作用

具体操作

①创建一块网卡 docker network create
②docker run 带上--network=xxxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.