Home > Software engineering >  Is there a simpler way to create an array in Go
Is there a simpler way to create an array in Go

Time:11-24

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)
}

CodePudding user response:

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}
  • Related