Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

561
Visualizações
strings.Contains in switch-case GoLang

Is it possible to use strings.Contains in switch case? something like:

func function(str string){
    switch str {
    case "str1":
        ...
    case strings.Contains("test"):
        ...
    default:
        ...
    }
}

Edit: its an example, thats not the real case I need it for. Im filtering many results and I want all results that contains X and I also have cases that I need to fully match.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do this:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "testString"
    switch {
    case strings.Contains(str, "test"):
        fmt.Println(true)
    default:
        fmt.Println(false)
    }
}

https://go.dev/play/p/_2fMd-3kE-r

switch without argument is like switch true.

over 4 years ago · Santiago Trujillo Relatório

0

Why do you need a switch for this boolean output?

Use the following inbuilt function.

func strings.Contains(s string, substr string) bool

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda