Go - Primitive Type
数值整型平台无关整型 在任何 CPU 架构和任何操作系统下面,长度都是固定不变的 有符号整型 vs 无符号整型 Go 采用补码作为整型的比特位编码方法:原码逐位取反后加 1 平台相关整型 长度会根据运行平台的改变而改变,在编写移植性要求的代码时,不要依赖下述类型的长度 123456a, b := int(5), uint(6)p := 0x12345678fmt.Println(unsafe.Sizeof(a)) // 8fmt.Println(unsafe.Sizeof(b)) // 8fmt.Println(unsafe.Sizeof(p)) // 8 溢出1234567var s int8 = 127s += 1fmt.Println(s) // -128var u uint8 = 1u -= 2fmt.Println(u) // 255 字面值1234567891011a := 53 // 十进制b := 0700 // 八进制,以 0 开头c1 := 0xaabbcc // 十六进制,以 0x 开头c2 := 0Xddeeff // 十六进制...
Security - OPA Operation
ConfigurationExample1$ opa run -s -c config.yaml 12345678910111213$ opa run --server \--log-format=json-pretty \--set=decision_logs.console=true \--set=services.A.url=http://my-bundles:8080/api/opa/bundle/ \--set=bundles.A.service=A \--set=bundles.A.polling.long_polling_timeout_seconds=45 \--set=bundles.A.resource=A.tar.gz \--set=services.B.url=http://my-bundles:8080/api/opa/bundle/ \--set=bundles.B.service=B \--set=bundles.B.polling.long_polling_timeout_seconds=45 \--set=bundles.B.resource=B.tar.gz \ ...
Security - OPA Management
Overview & Architecture OPA exposes a set of APIs that enable unified, logically centralized policy management. Read this page if you are interested in how to build a control plane around OPA that enables policy distribution and collection of important telemetry data like decision logs. OPA enables low-latency, highly-available policy enforcement by providing a lightweight engine for distributed architectures.By default, all of the policy and data that OPA uses to make decisions is kept in-memory...
Security - OPA Gatekeeper
Overview & Architecture In Kubernetes, Admission Controllers enforce policies on objects during create, update, and delete operations.Admission control is fundamental to policy enforcement in Kubernetes. For example, by deploying OPA as an admission controller you can Require specific labels on all resources. Require container images come from the corporate image registry. Require all Pods specify resource requests and limits. Prevent conflicting Ingress objects from being created. Admission ...
Security - OPA Core
Glance Use OPA for a unified toolset and framework for policy across the cloud native stack. Use OPA to decouple policy from the service’s code so you can release, analyze, and review policies without sacrificing availability or performance. Declarative Express policy in a high-level, declarative language that promotes safe, performant, fine-grained controls. Use a language purpose-built for policy in a world where JSON is pervasive. Iterate, traverse hierarchies, and apply 150+ built-ins like string man...
Go - Block + Scope
Variable Shadowing1234567891011121314151617package mainimport "fmt"var a = 11func foo(n int) { // 局部变量遮蔽了同名的包级变量 a := 1 a += n}func main() { fmt.Println(a) // 11 foo(5) fmt.Println(a) // 11} BlockExplicit Block 大括号包裹 12345678func foo() { // Block 1 { // Block 2 { // Block 3 { // Block 4 } } }} Explicit Block 是包裹在大括号内部的声明和语句序列 如果一对大括号内没有任何声明和其它语句,为空 Block Block 支持嵌套 Implic...
Go - Variable
内存边界 在编程语言中,为了方便操作内存特定位置的数据,使用变量与特定位置的内存绑定 编译器或者解析器需要知道变量所能引用的内存区域边界 动态语言 解析器可以在运行时通过对变量赋值的分析,自动确定变量的边界 一个变量可以在运行时被赋予大小不同的边界 静态语言 编译器必须明确知道一个变量的边界才允许使用该变量 但编译器无法自动分析,因此边界信息必须由开发者提供 - 变量声明 在具体实现层面,边界信息由变量的类型属性赋予 变量声明 Go 是静态语言,所有变量在使用前必须先进行声明声明:告诉编译器该变量可以操作的内存的边界信息(由变量类型信息提供) 通用 变量声明形式与主流静态语言的差异 - 将变量名放在了类型前面(方便语法糖移除 type) 如果没有显式为变量赋予初值,Go 编译器会为变量赋予类型零值 1var a int // a 的初值为 int 类型的零值 0 Go 的每种原生类型都有其默认值,即类型零值复合类型(array、struct)变量的类型零值为组成元素都为零值的结果 原生类型 类型零值 整型 0 浮点 0.0 布尔 FALSE 字符...
Go - Web
API 项目结构1234567891011121314151617$ tree.├── cmd│ └── bookstore│ └── main.go├── go.mod├── internal│ └── store│ └── memstore.go├── server│ ├── middleware│ │ └── middleware.go│ └── server.go└── store ├── factory │ └── factory.go └── store.go 逻辑结构 具体实现store 定义 Domain 和 Action store/store.go12345678910111213141516package storetype Book struct { Id string `json:"id"` Name string `json:"name"` Authors []string `json:"autho...
Go - Main + Init
main.main main 包中的 main 函数 - 所有可执行程序的用户层执行逻辑的入口函数 123456package main// 无参数 + 无返回值func main() { // 用户层执行逻辑} 可执行程序的 main 包必须定义 main 函数,否则会编译报错function main is undeclared in the main package main.go1package main 123$ go build main.go# command-line-argumentsruntime.main_main·f: function main is undeclared in the main package 在启动了多个 goroutine 的 Go 应用中,main.main 将在 Go 应用的主 goroutine 中执行 main.main 函数返回意味着整个 Go 程序结束 除了 main 包外,其它包也可以拥有自己的 main 函数 但依据 Go 的可见性规则,非 main 包中的 main 函数仅限于包内使用 123...
Go - Go Module
添加依赖 github.com/google/uuid main.go1234567891011package mainimport ( "github.com/google/uuid" "github.com/sirupsen/logrus")func main() { logrus.Println("hello, gomodule mode") logrus.Println(uuid.NewString())} go.mod 里的 require 字段中,没有任何 Module 提供了包 github.com/google/uuid 123$ go buildmain.go:4:2: no required module provides package github.com/google/uuid; to add it: go get github.com/google/uuid 手动 go get,下载依赖到本地 Module 缓存...















