replace
len(data) == 0
with data == ""
35 if err != nil {
36 return errors.WithStack(err)
37 }
38 if len(data) == 0 {39 *s = []string{}
40 return nil
41 }
replace
len(cfg.PushAddress) == 0
with cfg.PushAddress == ""
82
83// Push metrics in background.
84func Push(cfg *MetricConfig) {
85 if cfg.PushInterval.Duration == zeroDuration || len(cfg.PushAddress) == 0 { 86 log.Info("disable Prometheus push client")
87 return
88 }
replace
len(t) == 0
with t == ""
482// ParseTime parses a time string with the format "1694580288"
483// If the string is empty, it returns a zero time.
484func ParseTime(t string) (time.Time, error) {
485 if len(t) == 0 {486 return time.Time{}, nil
487 }
488 i, err := strconv.ParseInt(t, 10, 64)
replace
len(pathPrefix) == 0
with pathPrefix == ""
384 if _, ok := registerMap[pathPrefix]; ok {
385 return errs.ErrServiceRegistered.FastGenByArgs(pathPrefix)
386 }
387 if len(pathPrefix) == 0 {388 return errs.ErrAPIInformationInvalid.FastGenByArgs(group.Name, group.Version)
389 }
390 registerMap[pathPrefix] = handler
replace
len(componentName) == 0
with componentName == ""
144// GetComponentNameOnHTTP returns component name from Request Header
145func GetComponentNameOnHTTP(r *http.Request) string {
146 componentName := r.Header.Get(componentSignatureKey)
147 if len(componentName) == 0 {148 componentName = componentAnonymousValue
149 }
150 return componentName
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.