dqh19413 2018-01-12 12:58
浏览 340
已采纳

优雅的方法来检查多个字符串是否为空

How can I check if multiple strings are empty in an elegant way? This is how I currently do it:

//if one required field is empty, close the connection
    if (registerRequest.Email == "") ||
        (registerRequest.PhoneNumber == "")||
        (registerRequest.NachName =="") ||
        (registerRequest.VorName =="") ||
        (registerRequest.Password =="") ||
        (registerRequest.VerificationId ==""){

        //Could not proceed
        w.WriteHeader(UNABLE_TO_PROCEED)
        w.Write([]byte("Unable to register account."))
        return

    }
  • 写回答

2条回答 默认 最新

  • doulu3399 2018-01-12 13:16
    关注

    Note: You may use the solution below if you keep the "is-valid" condition in your handler, and also if you separate your condition into another function or method.

    You can create a simple helper function, which has a variadic parameter, and you can call it with any number of string values:

    func containsEmpty(ss ...string) bool {
        for _, s := range ss {
            if s == "" {
                return true
            }
        }
        return false
    }
    

    Example using it:

    if containsEmpty("one", "two", "") {
        fmt.Println("One is empty!")
    } else {
        fmt.Println("All is non-empty.")
    }
    
    if containsEmpty("one", "two", "three") {
        fmt.Println("One is empty!")
    } else {
        fmt.Println("All is non-empty.")
    }
    

    Output of the above (try it on the Go Playground):

    One is empty!
    All is non-empty.
    

    Your example would look like this:

    if containsEmpty(registerRequest.Email,
        registerRequest.PhoneNumber,
        registerRequest.NachName,
        registerRequest.VorName,
        registerRequest.Password,
        registerRequest.VerificationId) {
    
        // One of the listed strings is empty
    }
    

    Also registerRequest is a kinda long name, it could be shortened to like r. If you can't or don't want to rename it in the surrounding code and if you want to shorten the condition, you could also do something like this:

    If registerRequest is a pointer (or interface), you could also write:

    if r := registerRequest; containsEmpty(r.Email,
        r.PhoneNumber,
        r.NachName,
        r.VorName,
        r.Password,
        r.VerificationId) {
    
        // One of the listed strings is empty
    }
    

    Actually you can do this even if registerRequest is not a pointer, but then the struct will be copied. If registerRequest is a struct, then you can take its address to avoid having to copy it like this:

    if r := &registerRequest; containsEmpty(r.Email,
        r.PhoneNumber,
        r.NachName,
        r.VorName,
        r.Password,
        r.VerificationId) {
    
        // One of the listed strings is empty
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题