replace
len(s) == 0
with s == ""
80
81// StringToBytes converts string to slice of bytes without copy.
82func StringToBytes(s string) []byte {
83 if len(s) == 0 {84 return nil
85 }
86 return unsafe.Slice(unsafe.StringData(s), len(s))
Description
It is not recommended to use len
for empty string test.
A string can be tested for its emptiness either by treating it as a slice and calculating the length of the slice, or by treating it as a string and directly comparing the value. While both produce identical code when compiled, it makes more sense to treat a string as itself, than a slice, for the sake of comparison of values.
Bad practice
len(s) == 0
Recommended
s == ""
The recommended practice is considered more idiomatic in Go.