杨 发布的文章

文档(document)

  • ES是面向文档的,文档是数据的最小单位

    • 日志文件的日志项
    • 一条行记录
  • 文档会被序列化为json格式,保存在ES中

    • json对象由字段组成
    • 每个字段有各自的类型(字符串、数值、日期、布尔、二进制、范围)
  • 每个文档有一个unique ID

    • 可以自己指定
    • 也可以自动生成

文档元数据

  • 元数据用于标注文档的相关信息

    • _index - 文档所属的索引
    • _type - 文档所属的类型名
    • _id - 文档唯一ID
    • _source - 文档的原始json数据
    • _version - 文档的版本(用于做并发控制)
    • _score - 相关性打分

索引

  • index - 索引是文档的集合

    • index - 体现逻辑空间概念:每个索引都有自己mapping定义,用于定义文档的字段名和字段类型。
    • shard - 体现物理空间概念:索引的数据分散在shard上。
  • 索引的settings与mapping:

    • settings定义不同的数据分布
    • mapping定义文档字段类型

与MySQL类比

  • MySQL Table Row Column Schema SQL
  • ES index(type) Document Field Mapping DSL

区别:

  • ES适用于全文搜索、结果算分
  • MySQL适用于事务型、Join

REST API

# Click the Variables button, above, to create your own variables.
GET ${exampleVariable1} // _search
{
  "query": {
    "${exampleVariable2}": {} // match_all
  }
}

GET _cat/nodes?v

//查看索引相关信息
GET kibana_sample_data_ecommerce

//查看索引的文档总数
GET kibana_sample_data_ecommerce/_count

//查看indices
GET _cat/indices/.kibana?v&s=index

//查看状态为绿的索引
GET _cat/indices?v&health=green

//按文档个数排序
GET _cat/indices?v&s=docs.count:asc

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()

格式化输出