西维蜀黍

【Golang】指针(Pointers)

Background

Although Go absorbs many features from all kinds of other languages, Go is mainly viewed as a C family language. One evidence is Go also supports pointers. Go pointers and C pointers are much similar in many aspects, but there are also some differences between Go pointers and C pointers. This article will list all kinds of concepts and details related to pointers in Go.

  ...


【Golang】类型转换 - 获取变量类型

如果某个函数的入参的类型是 interface{},有下面几种方法可以获取入参的类型:

获取入参类型的方法

1 反射

import (
    "reflect"
    "fmt"
)
func main() {
    v := "hello world"
    fmt.Println(typeof(v))
}
func typeof(v interface{}) string {
    return reflect.TypeOf(v).String()
}

Example

其实,fmt.Sprintf("%T") 底层也是用反射实现的:

func (p *pp) printArg(arg interface{}, verb rune) {
	...
	// Special processing considerations.
	// %T (the value's type) and %p (its address) are special; we always do them first.
	switch verb {
	case 'T':
		p.fmt.fmtS(reflect.TypeOf(arg).String())
		return
  ...
  }
}  

Usage:

import "fmt"
func main() {
    v := "hello world"
    fmt.Println(typeof(v))
}
func typeof(v interface{}) string {
    return fmt.Sprintf("%T", v)
}
  ...


【Linux】CentOS 安装Docker

卸载旧版本

较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。

$ sudo yum remove docker \
         docker-client \
         docker-client-latest \
         docker-common \
         docker-latest \
         docker-latest-logrotate \
         docker-logrotate \
         docker-engine
  ...


【Protobuf】Protocol Buffers 入门

Protocol Buffers (Protobuf)

Protocol Buffers (Protobuf) is a free and open source cross-platform library used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

The method involves an interface description language that describes the structure of some data and a program that generates source code from that description for generating or parsing a stream of bytes that represents the structured data.

  ...


【Golang】使用 - 上传文件

单文件上传

我们使用multipart/form-data格式上传文件,利用c.Request.FormFile解析文件。

// HandleUploadFile 上传单个文件
func HandleUploadFile(c *gin.Context) {
	file, header, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": "文件上传失败"})
		return
	}

	content, err := ioutil.ReadAll(file)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
		return
	}

	fmt.Println(header.Filename)
	fmt.Println(string(content))
	c.JSON(http.StatusOK, gin.H{"msg": "上传成功"})
}
  ...


【Raspberry Pi】树莓派玩耍

查看树莓派系统的位数

安装软件的时候想查看树莓派系统是32位还是64位就出现了以下的操作,具体命令及作用如下:

getconf LONG_BIT        # 查看系统位数
uname -a            # kernel 版本
/opt/vc/bin/vcgencmd  version   # firmware版本
strings /boot/start.elf  |  grep VC_BUILD_ID    # firmware版本
cat /proc/version       # kernel
cat /etc/os-release     # OS版本资讯
cat /etc/issue          # Linux distro 版本
cat /etc/debian_version     # Debian版本编号

虽然树莓派3b的硬件支持64位的系统,但是官方的系统还是32位的,主要应该是为了兼容之前的硬件。

  ...


【Network】电信光猫内网穿透

Environmental Info

天翼网关-GPON,型号: PT921G

  ...


【Golang】使用 - Golang 使用 UUID

什么是 UUID?

UUID 是Universally Unique Identifier的缩写,即通用唯一识别码。

uuid的目的是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 uuid。

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.

  ...


【Golang】Golang 的环境变量

一句话概括,GOPATH中保存的是第三方依赖库的内容,GOROOT中保存的是原生的 go 工具的内容。

GOPATH

该环境变量的值为 Go 语言的工作区的集合(意味着可以有很多个)。工作区类似于工作目录。每个不同的目录之间用分隔。

工作区是放置 Go 源码文件的目录。一般情况下,Go 源码文件都需要存放到工作区中。

工作区一般会包含3个子文件夹,自己手动新建以下三个目录:src 目录,pkg 目录,bin 目录。

bin 目录里面存放的都是通过 go install 命令安装后,由 Go 命令源码文件生成的可执行文件( 在 Mac 平台下是 Unix executable 文件,在 Windows 平台下是 exe 文件)。

  ...


【Golang】依赖管理 - go module

Background

GOPATH的痛

当有多个项目时,不同项目对于依赖库的版本需求不一致时,无法在一个GOPATH下面放置不同版本的依赖项。典型的例子:当有多项目时候,A项目依赖C 1.0.0,B项目依赖C 2.0.0,由于没有依赖项版本的概念,C 1.0.0和C 2.0.0无法同时在GOPATH下共存,解决办法是分别为A项目和B项目设置GOPATH,将不同版本的C源代码放在两个GOPATH中,彼此独立(编译时切换),或者C 1.0.0和C 2.0.0两个版本更改包名。无论哪种解决方法,都需要人工判断更正,不具备便利性。

  ...