杨 发布的文章

package main
 
import s "strings"
import "fmt"
 
var p = fmt.Println
 
func main() {
    p("Contains: ", s.Contains("test", "es")) //是否包含 true
    p("Count: ", s.Count("test", "t")) //字符串出现字符的次数 2
    p("HasPrefix: ", s.HasPrefix("test", "te")) //判断字符串首部 true
    p("HasSuffix: ", s.HasSuffix("test", "st")) //判断字符串结尾 true
    p("Index: ", s.Index("test", "e")) //查询字符串位置 1
    p("Join: ", s.Join([]string{"a", "b"}, "-"))//字符串数组 连接 a-b
    p("Repeat: ", s.Repeat("a", 5)) //重复一个字符串 aaaaa
    p("Replace: ", s.Replace("foo", "o", "0", -1)) //字符串替换 指定起始位置为小于0,则全部替换 f00
    p("Replace: ", s.Replace("foo", "o", "0", 1)) //字符串替换 指定起始位置1 f0o
    p("Split: ", s.Split("a-b-c-d-e", "-")) //字符串切割 [a b c d e]
    p("ToLower: ", s.ToLower("TEST")) //字符串 小写转换 test
    p("ToUpper: ", s.ToUpper("test")) //字符串 大写转换 TEST
    p("Len: ", len("hello")) //字符串长度
    p("Char:", "hello"[1]) //标取字符串中的字符,类型为byte
}

ctx.SetCookie("ZJXUSS", res.ZJXUSS, 0, "/", ctx.Request.Host, false, true)

获取cookie

cookie, err := ctx.Cookie("ZJXUSS")

结合源码了解一下http协议里的cookie:

// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
    if path == "" {
        path = "/"
    }
    http.SetCookie(c.Writer, &http.Cookie{
        Name:     name,
        Value:    url.QueryEscape(value),
        MaxAge:   maxAge,
        Path:     path,
        Domain:   domain,
        SameSite: c.sameSite,
        Secure:   secure,
        HttpOnly: httpOnly,
    })
}

gin的实际是封装了go的cookie操作http.SetCookie,第二个参数传的cookie的结构:
// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
// HTTP response or the Cookie header of an HTTP request.
//
// See https://tools.ietf.org/html/rfc6265 for details.
type Cookie struct {
    Name  string
    Value string

    Path       string    // optional
    Domain     string    // optional
    Expires    time.Time // optional
    RawExpires string    // for reading cookies only

    // MaxAge=0 means no 'Max-Age' attribute specified.
    // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
    // MaxAge>0 means Max-Age attribute present and given in seconds
    MaxAge   int
    Secure   bool
    HttpOnly bool
    SameSite SameSite
    Raw      string
    Unparsed []string // Raw text of unparsed attribute-value pairs
}
name、value: 要在cookie保存的键值对
maxAge: max-age,cookie保存时间
 0: 不过期
-1: 不保存
>0: 保存多少秒
domain: 域名
secure: 防止信息传输泄漏
true: 只在https中传输,http不能传输 
false: 可以在https、http中传输
httpOnly: 禁止js读取,防止xss攻击

go向上取整方法 math.ceil(),该方法接收一个浮点型(float64)参数,如果小数不为0则会将小数舍去,小数点前数字加一。

如果除数和被除数都是int类型,需要手动转一下类型。

错误写法

batch := int(math.Ceil(float64(count / BATCH)))

正确写法

batch := int(math.Ceil(float64(count) / BATCH))

时间戳转日期

// 当前时间戳(time类型)
now := time.Now()
// 当天零点格式化日期
StartTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
// 当天零点时间戳(int64)
StartTimeStamp := StartTime.Unix()

// 指定时间戳转化为日期  
datetime = time.Unix(timestamp, 0).Format("2021-09-30 15:04:05")

日期格式转时间戳

// 日期字符串
date := "2021-09-30 15:04:05"
// 字符串转time类型
time := time.parse("2006-01-02 15:04:05", date)
// time转时间戳(int64)
timestamp := time.Unix()

if 条件运算符

•   not 非{<!-- -->{if not .condition}} {<!-- -->{end}}
•   and 与{<!-- -->{if and .condition1 .condition2}} {<!-- -->{end}}
•   or 或{<!-- -->{if or .condition1 .condition2}} {<!-- -->{end}}
•   eq 等于{<!-- -->{if eq .var1 .var2}} {<!-- -->{end}}
•   ne 不等于{<!-- -->{if ne .var1 .var2}} {<!-- -->{end}}
•   lt 小于 (less than){<!-- -->{if lt .var1 .var2}} {<!-- -->{end}}
•   le 小于等于{<!-- -->{if le .var1 .var2}} {<!-- -->{end}}
•   gt 大于{<!-- -->{if gt .var1 .var2}} {<!-- -->{end}}
•   ge 大于等于{<!-- -->{if ge .var1 .var2}} {<!-- -->{end}}

goroot go安装路径

gopath 项目路径

一般目录结构是
bin:编译后的二进制可执行文件
pkg:存放了go get的包的源码(我的是这样)
分为全局路径和项目路径

gomod

依赖管理通过go.mod文件来管理。
GO111MODULE 是一个开关,通过它可以开启或关闭 go mod 模式。

GO111MODULE=off禁用模块支持,编译时会从GOPATH和vendor文件夹中查找包。
GO111MODULE=on启用模块支持,编译时会忽略GOPATH和vendor文件夹,只根据 go.mod下载依赖。
GO111MODULE=auto,当项目在$GOPATH/src外且项目根目录有go.mod文件时,自动开启模块支持。

go env ex:

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/yangshuo/Library/Caches/go-build"
GOENV="/Users/yangshuo/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/yangshuo/git/zyb/go_project/mark/pkg/mod"
GONOPROXY="*.zuoyebang.cc"
GONOSUMDB="*.zuoyebang.cc"
GOOS="darwin"
GOPATH="/Users/yangshuo/git/zyb/go_project/mark"
GOPRIVATE="*.zuoyebang.cc"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.6/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/yangshuo/git/zyb/go_project/mark/markapi/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dn/2jhjt8k16y9c2yl7s1w8_1jr0000gn/T/go-build1386797669=/tmp/go-build -gno-record-gcc-switches -fno-common"

1. Substr(str string, start int, length int) string

a := "abcde"
b := Substr(a, 1, 2) 
fmt.Println(b)

// bc

2.[:]

a := "abcde"
b := a[1:3]
fmt.Println(b)

// bc

fmt.Sprintf()、Printf()、Fprintf()

fmt.Sprintf()

只格式化,不会输出到命令行。

fmt.Printf()

格式化输出

$pattern = "/^[\x{4e00}-\x{9fa5}]{2,10}$/u";

注意最后的u。

u (PCRE_UTF8)
此修正符打开一个与 perl 不兼容的附加功能。 模式字符串被认为是utf-8的. 这个修饰符 从 unix 版php 4.1.0 或更高,win32版 php 4.2.3 开始可用。 php 4.3.5 开始检查模式的 utf-8 合法性。
utf8格式的匹配模式字符串必须要使用 u 修饰符。 否则出现程序意料之外的异常情况。