总有那么一些老的,或高效的库是用 C/C++ 实现的,于是在其他语言中如果使用动态共享库就成了个问题。Java 要调用动态库需要用 JNI, 更快捷的话可使用第三方包装好的 JNI 调用库。在 Java 中要映射 C/C++ 的类型麻烦些,因为 Java 没有指针类型,所以从这方面来讲 Go 调用动态库幸许会更简单些。
下面我们自己在 Linux 下做一个动态库(.so 文件 - Shared Object),然在用 Go 来使用它。本文所用的操作系统为 Ubuntu20.04, 以 gcc 作为编译器。动态库的生成过程参考自 Linux动态库生成与使用指南 Read More
终于来到的 Go 的网络编程了,来写一个 TCP 服务端与客户端的程序。要用到 Go 语言的 net 包,是一个标准的 Listen+Accept 结构, 下面是一个简单的 TCP Server/Client 端的例子,启动了 Server 端口,可以用 telnet 去连接,也可以用 client.go 来连接 Read More
学到了 Go 语言本身的语法之后,开始步入 Go 的应用部分,掌握一个语言不得不接触的就是它对文件,网络的操作。
在正式进入主题之前不得不抱怨一下 Go 的官方编码规范中的代码缩进格式。曾经 Google 出的 Java 缩进规范是用两个空格(通常是用四个空格),并且是杜绝用 Tab 进行缩进的,因为 Tab 的宽度是不很确定的。
然而 Google 建议 Go 的代码格式化工具 gofmt 却反常规的强制使用 Tab 进行缩进,而且是。本人用 IntelliJ IDEA 编写 Go, 即使在 Go 的 Code Style 中把Use tab character去掉也是用 Tab 缩进,原因是在 Code Style > Go 的 Other 中有项 Run gofmt [] On code reformat, 这项也不能勾选。
不明白go fmt或gofmt为何强势而无理的用 Tab 进行缩进,看到多数地方以 8 个空格来显示一个 Tab,实在是难看,像下面这样子的 Read More
终于要开始了解 Go 的结构体和接口了。Go 的结构体只是一种纯粹的数据类型,而不像 C/C++ 的结构体里还能添加方法。Go 的 struct 更像是 Python 的 dataclass, 或 Java 的 record。Go 的结构体是值类型,通过 new 函数来创建,在 C/C++ 中,只要是 new 得到的就是指针。
结构体的字段名称也可以用_, 相当一个点位填充,字段也可以只有类型没有名称,称之为匿名字段。 Read More- Go 语方的数组声明方式别具一格,把 [] 看得太重了,看下面的各种声明方式再次强调 Go 的数组是值类型,作为函数参数传递会产生副本。避免传入数组产生副本消耗过多内存,办法是可传数组指针或用切片,切片是第一选择。
1var a = [...]int{18, 20} // 这是一个数组,如果省略 ..., 写成 []int{18, 20} 就是一个 slice 2var b = [5]string{"hello", 3: "ok"} //指定位置初始化 3var c = [...]float32{2.0, 2.3} // ... 可省略 4var d [20]int // d 的类型是 [20]int 5var e = new([20]int) // e 的类型是 *[20]int, 这是一个数组指针 6var g = [2][2][2]float64 // 多维数组 7var h = [...][5]int{{10,20}, {30,40}} // 类似其他语言一样,只有第一维才能用 [...]
数组的大小最大为 2GB,用==或!=比较两个数组时它们必须类型和长度一致。 Read More
Go 语言 2009 年面世,几个成功项目 Docker, Kubernetes, etcs, consul, flannel, 还有 Terraform。Go 编译运行快,听说学习也不难。安装好 Go 后,有两个环境变量很重要, GOPATH 是工作目录,GOROOT 是 Go 的安装目录, 如 /usr/local/go。GOPATH 允许多个目录,用 : 或 ;(Windows 下) 分隔,当 GOPATH 有多个目录,go get 命令的包放在第一个目录下。
$GOPATH 目录中约定的三个子目录- src: 存放 .go, .c, .h, .s 等源码文件,go run, go install 等命令也要在该目录下执行
- pkg: 存放编译时生成的中间文件,如 .a
- bin: 存放编译后的可执行文件
PM 2 是什么,官网上 https://pm2.keymetrics.io/ 是说 PM2(Process Manager 2) 是 Node JS 的高级的,产品级的进程管理工具。为什么叫做 PM2,那有没有 PM 或 PM 1 呢,没有,它起步就是 2. PM2 相对于 Node JS 有点类似于 Gunicorn 和 Python Web 应用的关系。 但 PM2 比 Gunicorn 功能更强大, 其实 PM2 还能用来管理其他任何进程,不局限于 Node JS 的。
最好的入门学习教程是官方的 Quick Start。从中我们可以大概看到 PM2 的主要应用:- 对进程的管理与监控
- 多种进程重启策略
- 日志的管理
- 使用配置文件
- 集群模式
- 用作静态 Web 服务
- 进行应用部署
- 使用环境变量
PM2 是 Node JS 应用,所以它是跨平台的。 Read More
Continue to advance to Kubernetes, the last article Docker Swarm In Action, After understanding Swarm, it is necessary to get familiar with Docker Compose. Docker Swarm forms a cluster of Docker hosts. As long as the Manager node is notified when the service is deployed, it will automatically find the corresponding node to run containers. Compose is another concept entirely. It organizes multiple associated containers into a whole for deployment, such as a load balance container, multiple web containers, and a Redis cache container to make up a whole bundle.
Revealed by its name, Compose, the concept of container orchestration was officially established. Later, Kubernetes, which we will learn, it's a tool for higher-level organization, operation and management of containers. Because Compose organizes the containers, it can start multiple associated containers with one command, instead of starting one container separately.
Regarding the installation of Docker Compose, Docker Desktop under Mac OS X comes with docker-compose; since Docker Compose is written in Python, we can usepip install docker-composeto install it, and use its commands after installationdocker-compose. Read More
Before officially entering into Kubernetes, I hope to know something about the earlier Docker Swarm, although it is basically no long used currently. Swarm provides a cluster of Docker hosts. There is at least one Manager (or more) in the cluster, plus 0 or more Workers. The main idea is that a Docker service (container) is executed by the Swarm cluster manager, and it will find the corresponding host in the cluster to execute the container. Both Manager and Worker can to run Docker containers, but only Manager can execute management commands.
The following two pictures shows the difference between running Docker containers on a host and a Swarm cluster host.
Run all containers in one host=>
Run container together
Many of my demos about Kafka, Docker, Python, Kubernates, and etc. are made with Vagrant virtual machines. It is time to write a blog for some frequently used Vagrant commands. Vagrant is a member of the HashiCorp family. Others of HashiCorp's famous tools include Terraform , Consul , Vault , Boundary , Packer , Nomad and Waypoint .
Speaking of Vagrant, I have to mention the similar Docker, in fact, they are quite different, while they both give people the external feeling that they are command line control Linux. Vagrant is essentially a virtual machine shell, allowing us to use Vagrant commands to interact with the virtual machine more conveniently, instead of switching back and forth between the host machine and the virtual machine, it is more convenient to manage multiple virtual machines in one single terminal; while Docker is a container, The essence of a container is a process on the host machine, but it is isolated from the file system, process, network, etc. of the process with a namespace, making the container process look like a virtual OS.
Vagrant is a tool for the development environment, and Docker is a tool for the deployment environment; Vagrant operates a standard Linux or Windows operating system, and the Docker is very critical on image size. Docker image is usually a trimmed system, just has necessary command to run our service. Since Vagrant corresponds to a virtual machine, the operation status with Vagrant and the installed software will be retained after Vagrant virtual machine shutdown, while the operations status in Docker are all for the current container (copy-on-write), which does not affect the corresponding image , Unless it is committed as a new image with docker commit.
Understand that Vagrant is just the shell of a virtual machine, so it requires different virtual machine implementations, such as VirtualBox, Hyper-V, VMware, etc., and we can use Vagrant to interact with Docker as well. Vagrant can support multiple Operation Systems. Read More