Is there a simpler way to create an array in Go

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

Is there a simpler way to create an array in Go

问题

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

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

  1. var service_filter []ble.UUID
  2. //s_uuid := ble.MustParse("00001820-0000-1000-8000-00805f9b34fb")
  3. s_uuid := ble.MustParse("1820")
  4. service_filter = append(service_filter, s_uuid)
  5. services, err := client.DiscoverServices(service_filter)
  6. for _, s := range services {
  7. fmt.Printf("%s\n", s.UUID)
  8. }

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

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

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:

  1. var service_filter []ble.UUID
  2. //s_uuid := ble.MustParse("00001820-0000-1000-8000-00805f9b34fb")
  3. s_uuid := ble.MustParse("1820")
  4. service_filter = append(service_filter, s_uuid)
  5. services, err := client.DiscoverServices(service_filter)
  6. for _, s := range services {
  7. fmt.Printf("%s\n", s.UUID)
  8. }

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

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

答案1

得分: 4

尝试这个:

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

在Go中初始化切片:

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

Try this

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

Initializing Slice in Go

  1. 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:

确定