go addressable 详解

Go语言规范中规定了可寻址(addressable)对象的定义,

For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.

对于一个对象x, 如果它的类型为T, 那么&x则会产生一个类型为*T的指针,这个指针指向x, 这是这一段的第一句话,也是我们在开发过程中经常使用的一种获取对象指针的一种方式。

阅读全文

10秒钟,让你的方法变为RPC服务

GitHub stars

rpcx一个服务治理的Go RPC框架, 拥有非常多的特性,支持跨语言的服务调用。 众多的特性可以参考doc.rpcx.io。它的服务治理的特性深受阿里巴巴的Dubbo框架的启发。

在实际的产品应用中,用户使用两台服务器+8台日志搜集服务(Client),轻松处理每天几十亿的服务调用, 除了中间一个路由器硬件闪断, 整个系统平稳运行多半年。 相比较之前Java的实现, 服务器节省了一半。 用户使用rpcx框架重构后的系统每月为公司节省了几十万元的成本。

rpcx框架的一个设计哲学就是简单。不希望用户需要花费大量的时间在框架的学习上,并且不需要proto文件或者重复且冗余的服务配置。最少只需要10行代码就可以创建一个服务, 如果需要额外的配置,也只需要几十行的代码。

虽然rpcx开发简单,但是作为开发人员来说,如果可以更加的偷懒, 那更是极好的一件事情了,这就是xgen开发的目的。

这个工具可以搜寻指定的package下可以配置成rpcx服务的类型, 并且生成一个服务器程序,将这些服务注册到服务器程序中。你可以指定是否需要zookeeperetcdconsul作为注册中心。

这个工具的开发参考了Go的tools的实现以及DigitalOcean公司的Fatih Arslan
开发的gomodifytags的实现。

阅读全文

使用二进制形式发布go package

我们在使用Go进行开发的时候, 经常会使用到第三方的库, 这时候我们一般都会通过go get到github.com、bitbucket或者自己私有库中去拉取第三库的源代码。 今天正好群里有网友问能不能将自己开发的库以二进制形式提供给用户,我就顺便整理了一下。

以二进制方式提供库的动机可能是为了保护自己公司的知识产权,也有可能是从安全的角度考虑,避免一些关键信息的泄漏等等,这不是本文讨论的范围。

阅读全文

[转]编写高性能的Go代码的最佳实践

原文: go-perfbook/performance

This document outlines best practices for writing high-performance Go code.

At the moment, it's a collection of links to videos, slides, and blog posts
("awesome-go-performance"), but I would like this to evolve into a longer book
format where the content is here instead of external. The links should be sorted into categories.

All the content will be licensed under CC-BY-SA.

阅读全文

年终盘点!2017年超有价值的Golang文章

马上就要进入2018年了,作为年终的盘点,本文列出了一些2017年的关于Go编程的一些文章,并加上简短的介绍。

文章排名不分先后, 文章也不一定完全按照日期来排列。我按照文章的大致内容分了类,便于查找。

文章主要从golangweekly、gocn每日新闻、medium、reddit、twitter、、知名博主的文章搜集而来。如果你发现好的2017年的Go文章没有列出来,欢迎在评论中粘帖出来,我会加入到文章正文中。

本文主要列出的是文章,2017年也涌现出来很多优秀的库和工具,但是不是本文要介绍的内容,所以没有列出来。

阅读全文

Go语言中实现基于 event-loop 网络处理

我们知道, Go语言为并发编程提供了简洁的编程方式, 你可以以"同步"的编程风格来并发执行代码, 比如使用go关键字新开一个goroutine。 对于网络编程,Go标准库和运行时内部采用 epoll/kqueue/IoCompletionPort来实现基于 event-loop的网络异步处理,但是通过netpoll的方式对外提供同步的访问。具体代码可以参考 runtime/netpollnetinternal/poll

Package poll supports non-blocking I/O on file descriptors with polling.
This supports I/O operations that block only a goroutine, not a thread.
This is used by the net and os packages.
It uses a poller built into the runtime, with support from the
runtime scheduler.

当然,我们平常不会设计到这些封装的细节,正常使用net包就很方便的开发网络程序了, 但是,如果我们想自己实现基于epollevent-loop网络程序呢?

阅读全文

[译]Go TCP Socket的实现

原文: TCP Socket Implementation On Golang by Gian Giovani.

译者注: 作者并没有从源代码级别去分析Go socket的实现,而是利用strace工具来反推Go Socket的行为。这一方法可以扩展我们分析代码的手段。
源代码级别的分析可以看其实现: net poll,以及一些分析文章:The Go netpoller, The Go netpoller and timeout

Go语言是我写web程序的首选, 它隐藏了很多细节,但仍然不失灵活性。最新我用strace工具分析了一下一个http程序,纯属手贱但还是发现了一些有趣的事情。

下面是strace的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
91.24 0.397615 336 1185 29 futex
4.13 0.018009 3 7115 clock_gettime
2.92 0.012735 19 654 epoll_wait
1.31 0.005701 6 911 write
0.20 0.000878 3 335 epoll_ctl
0.12 0.000525 1 915 457 read
0.02 0.000106 2 59 select
0.01 0.000059 0 170 close
0.01 0.000053 0 791 setsockopt
0.01 0.000035 0 158 getpeername
0.01 0.000034 0 170 socket
0.01 0.000029 0 160 getsockname
0.01 0.000026 0 159 getsockopt
0.00 0.000000 0 7 sched_yield
0.00 0.000000 0 166 166 connect
0.00 0.000000 0 3 1 accept4
------ ----------- ----------- --------- --------- ----------------
100.00 0.435805 12958 653 total

阅读全文