Golang Web框架Buffalo 简单使用

官方文档 https://gobuffalo.io 安装 安装要求 Before installing make sure you have the required dependencies installed: A working Go environment Go version v1.16.0. Frontend Requirements# The following requirements are optional. You don’t need them if you want to build an API or if you prefer to build your app in an old-fashioned way. node version 8 or greater either yarn or npm for the asset pipeline built upon webpack. Database Specific Requirements# Again, if you don’t need a database, you won’t need these....

从Golang的开源项目中学习不同的功能实现

缘起 最近看到有些go开源项目中的代码,看到其中的功能,故整理备用。 密码 阈值解密(Threshold Decryption) 来源 取自公众号文章 package main import ( "crypto/rand" "fmt" "github.com/hashicorp/vault/shamir" ) func main() { // 1. 生成一个随机密钥(如AES-256密钥) secretKey := make([]byte, 32) _, err := rand.Read(secretKey) if err != nil { panic(err) } fmt.Printf("原始密钥: %x\n", secretKey) // 2. 分片为3份,至少需要2份恢复(阈值k=2,总分片n=3) shards, err := shamir.Split(secretKey, 3, 2) if err != nil { panic(err) } // 3. 模拟分片存储(实际中分发给不同机构) shard1, shard2, shard3 := shards[0], shards[1], shards[2] fmt.Printf("分片1: %x\n分片2: %x\n分片3: %x\n", shard1, shard2, shard3) // 4....

使用protoc-gen-star编写protoc插件

预备知识 需要安装的软件 protoc golang go 软件包 github.com/lyft/protoc-gen-star 插件调用步骤 protoc,PB编译器,使用一组标志(记录在protoc -h下)进行配置,并将一组文件作为参数交给它。在这种情况下,I标志可以被多次指定,是它在proto文件中用于导入依赖关系的查找路径。默认情况下,官方描述符protos已经被包含在内。 myplugin_out 告诉 protoc 使用 protoc-gen-myplugin protoc-plugin。这些插件会从系统的 PATH 环境变量中自动解析,或者可以用另一个标志明确指定。官方的protoc-plugins (例如,protoc-gen-python) 已经在protoc注册了。该标志的值是特定于特定插件的,但 :…/generated 后缀除外。这个后缀表示protoc将把该包生成的文件放在哪个根目录下(相对于当前工作目录)。然而,这个生成的输出目录不会传播给 protoc-gen-myplugin,所以它需要在标志的左边重复。PG* 通过一个 output_path 参数支持这一点。 protoc 解析传入的 proto 文件,确保它们在语法上是正确的,并加载任何导入的依赖项。它将这些文件和依赖关系转换成描述符 (它们本身就是 PB 消息),并创建一个 CodeGeneratorRequest (又是一个 PB)。protoc 将这个请求序列化,然后执行每个配置的 protoc-plugin,通过 stdin 发送有效载荷。 protoc-gen-myplugin 启动,接收请求的有效载荷,并将其解密。一个基于 PG* 的 protoc-plugin 有两个阶段。首先,PG* 对从 protoc 收到的 CodeGeneratorRequest 进行解密,并为每个文件和其包含的所有实体创建一个完全连接的抽象语法树 (AST)。为这个插件指定的任何参数也会被解析,以便以后使用。 当这一步完成后,PG*就会执行任何注册的模块,把构建的AST交给它。模块可以被写成生成人工制品(例如,文件),或者只是对所提供的图进行某种形式的验证而没有任何其他副作用。模块在针对PB的操作方面提供了极大的灵活性。 一旦所有的模块都被运行,PG*会将任何自定义的工件写入文件系统,或者将生成器特定的工件序列化为CodeGeneratorResponse并将数据发送到其stdout。这整个流程看起来像这样。 foo.proto → protoc → CodeGeneratorRequest → protoc-gen-myplugin → CodeGeneratorResponse → protoc → foo.pb.go 假设插件名称为diy,则需要编译程序为protoc-gen-diy,并将程序加入系统Path变量,通过下面的命令调用插件。 protoc -I ....

golang http Reverse Proxy使用备忘

创建 一般使用 使用 httputil.NewSingleHostReverseProxy 即可 返回Response 当我们想实现获取通过ReverseProxy的请求结果时,可以使用自定义的 responsewriter 来实现。参考定义 func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { .... } type ResponseWriter interface { // Header returns the header map that will be sent by // WriteHeader. The Header map also is the mechanism with which // Handlers can set HTTP trailers. // // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are // trailers....

tailscale的泛型SingleFlight

源地址 tailscale仓库 完整代码如下 // Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package singleflight provides a duplicate function call suppression // mechanism....

Golang 默认的CGO参数编译导致的GLIBC错误

问题描述 使用go正常编译了Linux下的程序,放到服务器上报错 ./app: /lib64/libc.so.6: version `GLIBC_2.34' not found (required by ./app) 解决 Google了下,发现相同的Issue,于是通过go env检查本机golang运行环境,发现CGO默认启用而且程序也不涉及CGO相关的东西,于是设置CGO参数为关闭。然后编译程序 CGO_ENABLED="0" go build -v 重新上传,运行OK.

golang http客户端使用自定义dns

摘自互联网 原文 package main import ( "context" "io/ioutil" "log" "net" "net/http" "time" ) func main() { var ( dnsResolverIP = "8.8.8.8:53" // Google DNS resolver. dnsResolverProto = "udp" // Protocol to use for the DNS resolver dnsResolverTimeoutMs = 5000 // Timeout (ms) for the DNS resolver (optional) ) dialer := &net.Dialer{ Resolver: &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ Timeout: time.Duration(dnsResolverTimeoutMs) * time....

golang转换任意长度[] byte为int

package main import ( "encoding/binary" "fmt" ) func main() { slices := [][]byte{ {1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7, 8}, } for _, s := range slices { fmt.Println(getInt1(s), getInt2(s)) } } func getInt1(s []byte) int { var b [8]byte copy(b[8-len(s):], s) return int(binary....

Protobuf golang小札

Oneof 如果您有许多字段的消息,并且最多可以同时设置一个字段,则可以使用Oneof功能来执行此行为并保存内存。一个字段就像常规字段一样,除了单一共享内存中的所有字段,最多可以同时设置一个字段。设置Oneof的任何成员都会自动清除所有其他成员。 ​ Google protobuf 文档#Oneof 示例proto 创建protoOneof.proto 的proto文件 syntax = "proto3"; package oneof_test; option go_package ='.;oneof'; message WeiboUser{ string user_id = 1; string user_nick = 2; } message DouyinUser{ string auth_token = 1; string nick_name = 2; } message User{ oneof user_source{ string weibo_url = 1; string douyin_url = 2; } oneof user_info{ WeiboUser weibo_user_info = 3; DouyinUser douyin_user_info = 4; } } 使用命令生成go代码 protoc --proto_path=. --go_out=paths=source_relative:./oneof ./protoOneof.proto 生成的protoOneof.pb.go代码如下:...

使用gotests生成表驱动测试

使用gotests可以很方便的生成表驱动测试代码,表驱动测试的具体内容,请参考go官方的wiki。下面是具体的使用方法。 安装 使用下面命令进行安装 go install github.com/cweill/gotests/gotests@latest 如果是go1.16之前的版本,可以使用命令 go get -u github.com/cweill/gotests/...来进行安装。 使用 gotests支持的参数如下: Usage of C:\Users\czyt\go\bin\gotests.exe: -all generate tests for all functions and methods -excl string regexp. generate tests for functions and methods that don't match. Takes precedence over -only, -exported, and -all -exported generate tests for exported functions and methods. Takes precedence over -only and -all -i print test inputs in error messages -nosubtests disable generating tests using the Go 1....