• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

517
Views
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.

about 3 years ago · Santiago Trujillo
2 answers
Answer question

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.

about 3 years ago · Santiago Trujillo Report

0

Why do you need a switch for this boolean output?

Use the following inbuilt function.

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

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error