NVIDIA / go-nvml

Go Bindings for the NVIDIA Management Library (NVML)
Apache License 2.0
290 stars 62 forks source link

Return golang errors instead of nvml.Return values #125

Open elezar opened 2 months ago

elezar commented 2 months ago

These changes move from using nvml.Return values to using native Golang error values, making code more idiomatic.

Note that this requires that error.Is is used instead of checking for equality in the returned value.

For example:

current, pending, err := device.GetMigMode()
if errors.Is(err, nvml.ERROR_NOT_SUPPORTED) {
    fmt.Printf("MIG is not supported for device %v\n", i)

} else if err != nil {
    log.Fatalf("Error getting MIG mode for device at index %d: %v", i, err)
}
fmt.Printf("MIG mode for device %v: current=%v pending=%v\n", i, current, pending)

would become:

current, pending, err := device.GetMigMode()
if errors.Is(err, nvml.ERROR_NOT_SUPPORTED) {
    fmt.Printf("MIG is not supported for device %v\n", i)

} else if err != nil {
    log.Fatalf("Error getting MIG mode for device at index %d: %v", i, err)
}
fmt.Printf("MIG mode for device %v: current=%v pending=%v\n", i, current, pending)
klueska commented 2 months ago

Your two example's in the description are the same .. I think you wanted one of them to check euqility instead of using errors.Is().

That said -- what will happen if someone does do err == nvml.ERROR_NOT_SUPPORTED? Will it be false even when errors.Is(err, nvml.ERROR_NOT_SUPPORTED) would be true?