侧边栏壁纸
博主头像
Tony's Blog博主等级

行动起来,coding

  • 累计撰写 83 篇文章
  • 累计创建 58 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
go

github上好用的库(持续更新)

Tony
2024-02-21 / 0 评论 / 0 点赞 / 48 阅读 / 3557 字

本文链接: https://blog.csdn.net/lishuangquan1987/article/details/122545384

1.go操作modubus库

https://github.com/things-go/go-modbus

https://github.com/goburrow/modbus

推荐使用things-go的库,因为他的API更人性化一点,例如:

https://github.com/goburrow/modbus:写入寄存器的值还要自己转换byte,还有返回byte,什么鬼

// WriteMultipleRegisters writes a block of contiguous registers
// (1 to 123 registers) in a remote device and returns quantity of
// registers.
WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error)

https://github.com/things-go/go-modbus:这个库就好的多,不用自己转换

// WriteMultipleRegisters writes a block of contiguous registers
// (1 to 123 registers) in a remote device and returns success or failed.
WriteMultipleRegisters(slaveID byte, address, quantity uint16, value []uint16) error

使用自己封装的库:https://github.com/lishuangquan1987/modbusplus

2.go-linq

使用这个库,可以像C#操作linq一样写go

package main

import (
	"fmt"

	"github.com/ahmetb/go-linq/v3"
)

func main() {
	people := make([]Person, 0)

	people = append(people, Person{
		Name: "tony",
		Age:  20,
	})
	people = append(people, Person{
		Name: "li",
		Age:  20,
	})
	people = append(people, Person{
		Name: "tony1",
		Age:  30,
	})
	people = append(people, Person{
		Name: "li1",
		Age:  30,
	})

	result := make([]linq.Group, 0)

	linq.From(people).GroupBy(func(x interface{}) interface{} { return x.(Person).Age }, func(x interface{}) interface{} { return x }).ToSlice(&result)

	for _, group := range result {
		fmt.Println(group.Key)

		for _, g := range group.Group {
			fmt.Printf("Name:%s,Age:%d\n", g.(Person).Name, g.(Person).Age)
		}
	}

}

type Person struct {
	Name string
	Age  int
}

0

评论区