Is there a simpler way to create an array in Go

huangapple go评论140阅读模式
英文:

Is there a simpler way to create an array in Go

问题

我正在尝试学习Go语言,并且正在将我之前用Python写的东西重新实现为一个项目。我正在尝试向一个蓝牙LE设备发送一些基本命令。最终,我想要一个可写入的Characteristic,而为了使用BLE库实现这一点,我首先需要建立连接,找到服务,并筛选出感兴趣的服务,然后获取该服务的Characteristics。这一切都很好。

我想知道创建获取感兴趣的服务的筛选数组的方式是否是最佳的:

var service_filter []ble.UUID

//s_uuid := ble.MustParse("00001820-0000-1000-8000-00805f9b34fb")
s_uuid := ble.MustParse("1820")
service_filter = append(service_filter, s_uuid)

services, err := client.DiscoverServices(service_filter)
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}

我特别问的是"service_filter"。在其他语言中,我可能会这样做:

services, err := client.DiscoverServices([ ble.MustParse("1820") ])
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}
英文:

I am trying to learn Go, and I am reimplementing something I have written in Python as a project. I am trying to send some basic commands to a Bluetooth LE device. Ultimately, I want a Characteristic I can write to, and it seems in order to do that with the BLE library, I first need to get a connection, find the services, filtering to the one of interest, and then once I have the Service, get its characteristics. That's all fine.

I am wondering if this is the best way of creating the filter array for getting the service of interest though:

var service_filter []ble.UUID

//s_uuid := ble.MustParse("00001820-0000-1000-8000-00805f9b34fb")
s_uuid := ble.MustParse("1820")
service_filter = append(service_filter, s_uuid)

services, err := client.DiscoverServices(service_filter)
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}

I am specifically asking about "service_filter". In other languages, I might do the following:

services, err := client.DiscoverServices([ ble.MustParse("1820") ])
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}

答案1

得分: 4

尝试这个:

services, err := client.DiscoverServices([]ble.UUID{ble.MustParse("1820")})
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}

在Go中初始化切片:

var a = []int{1,2,3}
英文:

Try this

services, err := client.DiscoverServices([]ble.UUID{ble.MustParse("1820")})
for _, s := range services {
    fmt.Printf("%s\n", s.UUID)
}

Initializing Slice in Go

var a = []int{1,2,3}

huangapple
  • 本文由 发表于 2021年11月23日 23:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/70083976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定