What is the best way to test for an empty string in Go?

前端 未结 10 664
终归单人心
终归单人心 2021-01-29 20:26

Which method is best (more idomatic) for testing non-empty strings (in Go)?

if len(mystring) > 0 { }

Or:

if mystring != \"\"         


        
10条回答
  •  孤街浪徒
    2021-01-29 20:35

    It would be cleaner and less error-prone to use a function like the one below:

    func empty(s string) bool {
        return len(strings.TrimSpace(s)) == 0
    }
    

提交回复
热议问题