go

How to change external variable's value inside a goroutine closure

若如初见. 提交于 2020-12-30 04:01:29
问题 func (this *l) PostUpload(ctx *Context) { //ctx.Response.Status = 500 l, err := models.NewL(this.Config) go func() { err = l.Save(file) if err != nil { ctx.Response.Status = 500 ctx.Response.Body = err } else { ctx.Response.Status = 204 } }() } How to change ctx.Response.Status value inside the goroutine closure? 回答1: You have no guarantee to observe changes made to the value of a variable in another goroutine without synchronization . See The Go Memory Model for details. So if you want to

聊聊klog的header

谁说我不能喝 提交于 2020-12-30 02:00:05
序 本文主要研究一下klog的header println k8s.io/klog/v2@v2.4.0 /klog.go func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) { buf, file, line := l.header(s, 0) // if logr is set, we clear the generated header as we rely on the backing // logr implementation to print headers if logr != nil { l.putBuffer(buf) buf = l.getBuffer() } if filter != nil { args = filter.Filter(args) } fmt.Fprintln(buf, args...) l.output(s, logr, buf, file, line, false) } println方法先执行l.header(s, 0),若logr不为nil则先l.putBuffer(buf),然后重新设置buf header k8s.io/klog/v2@v2.4.0 /klog.go func (l

真香系列之 Golang 升级

北战南征 提交于 2020-12-30 00:52:47
Golang 以前的依赖管理一直饱受诟病,社区的方案也层出不穷,比如 vendor, glide, godep 等。之前的依赖管理一直是依靠 GOPATH 或者将依赖代码下载到本地,这种方式都有劣势。另外由于特殊的网络环境,导致谷歌的大部分包都没有办法下载。从 Golang 1.11 开始,官方已内置了更为强大的 Go modules 来一统多年来 Go 包依赖管理混乱的局面,从 1.13 开始将成为默认配置。配合 Goproxy 来使用来说,真香。这次配合我之前的 Golang 开源项目 GShark 升级到 1.13,升级花费的时间不超过 5 分钟,体验优秀。 升级 Golang 版本 其实升级 Golang 版本是非常简单的,只要移除之前的 Golang,然后复制新版本的 Golang 就可以了。以我之前的 VPS 为例(CentOS,亲测苹果系统可以使用同样的方式升级),之前安装的 Golang 版本是 1.9。 1.移除旧版本 Golang rm -rf /usr/local/go 2.安装新版本 Golang wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz 3.配置 Golang 环境 如果你之前配置过

至联云带你看IPFS最新进展:以太经典与Filecoin合作,Go-ipfs 0.7 发布

对着背影说爱祢 提交于 2020-12-29 16:42:55
以太经典与Filecoin达成合作 以太坊经典实验室(Ethereum Classic Labs)正在与 Filecoin 合作,Filecoin 是一个旨在存储人类最重要信息的去中心化存储网络。这项合作将带来多种语言,并首次为开发人员提供使用 OpenRPC 的 Filecoin Lotus API 现代界面。 OpenRPC OpenRPC是由其 ETC 核心开发团队创建的 ETC Labs 开源项目,它为 JSON-RPC 2.0 API 定义了与编程语言无关的标准接口描述。与 Filecoin Lotus 的集成将为开发人员提供适当的文档,并使以最小的占用空间支持自动类型安全客户端生成和文档成为可能。同时自动跟踪 API 中的实现更改。 Filecoin Lotus API上的OpenRPC意味着: 使开发人员、测试人员、架构师和技术作者保持同步的通用词汇表和文档;用多种语言生成的服务器存根/骨架;用多种语言生成的客户端;用多种语言生成的模拟服务器;用多种语言生成的测试;文档生成。 IPFS社区分享挑战与荣耀 IPFS 社区再次开会,分享荣耀和挑战的故事! 1、金Go-ipfs 0.7发布预览 主持人 Jacob Heun 先做了一些社会和对行为准则的回顾,然后介绍了第一位演讲者,go-ipfs 负责人 Adin Schmahmann,他对 go-ipfs 0.7

GO编程模式系列(一):切片,接口,时间和性能

有些话、适合烂在心里 提交于 2020-12-29 15:56:05
在本篇文章中,我会对Go语言编程模式的一些基本技术和要点,这样可以让你更容易掌握Go语言编程。其中,主要包括,数组切片的一些小坑,还有接口编程,以及时间和程序运行性能相关的话题。 本文是全系列中第1 / 9篇:Go编程模式 Go编程模式:切片,接口,时间和性能 Go 编程模式:错误处理 Go 编程模式:Functional Options Go编程模式:委托和反转控制 Go编程模式:Map-Reduce Go 编程模式:Go Generation Go编程模式:修饰器 Go编程模式:Pipeline Go 编程模式:k8s Visitor 模式 目录 Slice深度比较接口编程 接口完整性检查 时间 性能提示 参考文档 Slice 首先,我们先来讨论一下Slice,中文翻译叫“切片”,这个东西在Go语言中不是数组,而是一个结构体,其定义如下: type slice struct{ array unsafe.Pointer //指向存放数据的数组指针 len int //长度有多大 cap int //容量有多大 } 用图示来看,一个空的slice的表现如下: 熟悉C/C++的同学一定会知道,在结构体里用数组指针的问题——数据会发生共享!下面我们来看一下slice的一些操作: foo = make([]int, 5) foo[3] = 42 foo[4] = 100 bar :=

Read multiple time a Reader

痴心易碎 提交于 2020-12-29 07:53:27
问题 I have two http handlers that use the same http.ResponseWriter and *http.Request and read the request body like this: func Method1 (w http.ResponseWriter, r *http.Request){ var postData database.User if err := json.NewDecoder(r.Body).Decode(&postData); err != nil { //return error } } func Method2 (w http.ResponseWriter, r *http.Request){ var postData database.User //this read gives (of course) EOF error if err := json.NewDecoder(r.Body).Decode(&postData); err != nil { //return error } }

Golang - Docker API - parse result of ImagePull

你。 提交于 2020-12-29 06:44:58
问题 I'm developing a Go script that uses the Docker API for the purposes of my project. After I login to my repository, I pull the Docker image I want, but the problem is that the ImagePull function returns an instance of io.ReadCloser, which I'm only able to pass to the system output via: io.Copy(os.Stdout, pullResp) It's cool that I can see the response, but I can't find a decent way to parse it and implement a logic depending on it, which will do some things if a new version of the image have

Golang - Docker API - parse result of ImagePull

感情迁移 提交于 2020-12-29 06:44:38
问题 I'm developing a Go script that uses the Docker API for the purposes of my project. After I login to my repository, I pull the Docker image I want, but the problem is that the ImagePull function returns an instance of io.ReadCloser, which I'm only able to pass to the system output via: io.Copy(os.Stdout, pullResp) It's cool that I can see the response, but I can't find a decent way to parse it and implement a logic depending on it, which will do some things if a new version of the image have

Is “google/protobuf/struct.proto” the best way to send dynamic JSON over GRPC?

限于喜欢 提交于 2020-12-29 05:49:31
问题 I have a written a simple GRPC server and a client to call the server (both in Go). Please tell me if using golang/protobuf/struct is the best way to send a dynamic JSON with GRPC. In the example below, earlier I was creating Details as a map[string]interface{} and serializing it. Then I was sending it in protoMessage as bytes and was de-serializing the message on the server side. Is it the best/efficient way to do it or should I define Details as a struct in my proto file? Below is User

Is “google/protobuf/struct.proto” the best way to send dynamic JSON over GRPC?

醉酒当歌 提交于 2020-12-29 05:48:19
问题 I have a written a simple GRPC server and a client to call the server (both in Go). Please tell me if using golang/protobuf/struct is the best way to send a dynamic JSON with GRPC. In the example below, earlier I was creating Details as a map[string]interface{} and serializing it. Then I was sending it in protoMessage as bytes and was de-serializing the message on the server side. Is it the best/efficient way to do it or should I define Details as a struct in my proto file? Below is User