vendredi 7 juin 2019

"return" not works, "else...return" works

I have a big trouble with GoLang, I'm writing my own Router (also to learn how to code in Go)...

I have this piece of code:

  paramSegsLen := *lenght of an array*
  for i := 0; i < paramSegsLen; i++ {
  paramSeg := segs[i]
  routeSeg := rtr.segs[i]

    if routeSeg != paramSeg {
      if strings.HasPrefix(routeSeg, ":") {
        routeSeg = strings.TrimPrefix(routeSeg, ":")
        ctx = context.WithValue(ctx, ctxKey(routeSeg), paramSeg) // <- [1]
      } else if strings.HasSuffix(routeSeg, "...") {
        if strings.HasPrefix(paramSeg, routeSeg[:len(routeSeg)-3]) {
            return ctx, true
        }
      } else { // <------------------ [2]
        return nil, false // <-------  + 
      } // <-------------------------  +
    }
  }

One of you guys could explain to me why this "else...return"[2] works very well, but when I omit the "else"[2] the IDE notify me that the line [1] will be unreachable?


Thanks and srr for my English

Aucun commentaire:

Enregistrer un commentaire