go: 读取文件

学习网站: https://gobyexample.com/reading-files 可以直接读取文件里面的所有内容到内存里面: data, err := os.ReadFile("/tmp/data") fmt.Println(string(data)) 有时候想要自定义一些操作,比如读取文件里面的某些内容,从某个位置开始读取等,这时候,我们可以用 os.Open 打开一个文件,返回一个 os.File 对象。 f, err := os.Open("/tmp/data") 我们可以定义一个缓存(5个字节的[]byte类型),然后从文件中读取内容,读取的内容大小上限为5: b1 := make([]byte, 5) n1, err := f.Read(b1) fmt.Printf("%d bytes read from file: %s\n", n1, string(b1)) 调用 f.Read来读取文件,返回两个结果,第一个为实际读取的内容长度,第二个为error。我们定义了一个长度为5的 b1 来存储文件内容,但是文件里面的内容长度可能只有3,因此,n1不一定等于5,它是实际读取的长度。 同样,我们还能用 os.Seek,来查找文件的位置,第一个参数为 offset,意味着偏移量,第二个参数为 whence, 0表示从文件的开始位置进行偏移查找 1表示从文件的当前位置开始偏移,如果之前已经设置过偏移,再次设置则从当前位置开始偏移 2表示从文件的末尾开始偏移 o2, err := f.Seek(6, 0) b2 := make([]byte, 2) n2, err := f.Read(b2) fmt.Printf("%d bytes read at %d\n", n2, o2) fmt.Printf("Value is : %s\n", string(b2[:n2])) golang 的 io包提供了一些有用的函数,帮助我们高效读取。比如 io....

April 18, 2022 · 2 min · LingZihuan

go: grpc和protobuf

gRPC 一个高性能、开源的通用RPC框架。 gPPC是一个现代的开源高性能的远程过程调用框架,并且可以运行在任何环境中。它可以有效地连接数据中心内和跨数据中心的服务,支持负载均衡、跟踪、健康检查和身份验证。它也适用于分布式计算的最后一英里,将设备、移动应用程序和浏览器连接到后端服务。 gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services. github 地址: https://github.com/grpc/grpc 原来是用 C++ 写的,后来也有了go语言版本:https://github....

April 16, 2022 · 2 min · LingZihuan

go 和 protobuf

Google protocol buffers 简称protobuf,它是谷歌提供的跨语言,跨平台的,可扩展的用来将结构化的数据进行序列化的一种机制。就像XML,但是更加精简、快速和简单。我们可以先预先定义我们的协议(写到一个 .proto 文件里面),里面声明了我们需要怎么去将数据结构化,然后,我们可以使用这份proto,去生成我们指定的语言源代码文件,我们就能够很方便地从各种数据流中读取结构化数据,或者是写入结构化数据。并且是跨语言的。 Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. 安装protoc编译器 ....

April 14, 2022 · 2 min · LingZihuan

Go 使用mongodb

安装 mkdir go-mongo go mod init go-mongo go get go.mongodb.org/mongo-driver/mongo 使用 数据库里面有一条这样的数据: { "_id": { "$oid": "6252912ec4495f97bccf41aa" }, "title": "My Mongo Post", "create_time": { "$date": "2022-04-10T10:33:45.149Z" }, "viewer": 201 } 连接数据库 // 关键代码 const MONGO_URI = "mongodb://localhost:27017/test" client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(MONGO_URI)) // 优雅关闭连接 defer client.Disconnect(context.TODO()) 数据查询 首先获取到对应的database以及collection coll := client.Database("test").Collection("post") 然后查询: coll := client.Database("test").Collection("post") var result bson.M // 需要一个bson.M 对象,用于存储查询回来的数据 // FindOne 接受2个参数,一个context,一个filter,filter 为 bson....

April 10, 2022 · 2 min · LingZihuan

go:简简单单的并发网络连接

go 是天生支持并发的,我们只要 使用 go func() 就可以快速实现并发。在网络服务的处理中,实现并发可以大大提高服务的吞吐量,我们来研究一下。 简单的TCP服务器 我们先来实现一个简单的tcp服务,监听8989端口,从连接中读取一段数据,这段数据表示的是当前请求的id,然后返回一段话。 服务器 server/main.go package main import ( "fmt" "log" "net" ) func main() { // 启动tcp连接,监听8989端口 l, err := net.Listen("tcp", ":8989") if err != nil { log.Fatalf("Server listening error: %s\n", err) } for { conn, err := l.Accept() if err != nil { log.Printf("Accept error: %s\n", err) continue } // 接收连接,调用handleConn处理当前连接 handleConn(conn) } } func handleConn(conn net....

April 6, 2022 · 6 min · LingZihuan