Home > OS >  return dinamic data string in golang
return dinamic data string in golang

Time:11-09

package main

import (
    "fmt"
)

func FindSimilarData(input string, data ...string) string {
same := ""
    for _, d := range data {
        fmt.Println(d)

        if (d) == input {
            same  = d
            fmt.Println(d)
            fmt.Println(same)
        }
    }
    return "" 
}

func main() {
    fmt.Println(FindSimilarData("iphone", "laptop", "iphone 13", "iphone 12", "iphone 12 pro"))
}

i want to return the data type string which has similar data, i was trying to use strings. Contains but it only returns the boolean type. can anyone give an example for this one, thanks before

CodePudding user response:

If strings.Contains returns true you can append it on a output string and return the string.

func FindSimilarData(input string, data ...string) string {
    same := ""
    for _, d := range data {
        fmt.Println(d)

        if strings.Contains(d, input) {
            if len(same) != 0 {
                same  = ","
            }
            same  = d
        }
    }
    return same
}

CodePudding user response:

package main

import (
    "fmt"
    "strings"
)

func FindSimilarData(input string, data ...string) string {
    same := ""
    for _, d := range data {
        fmt.Println(d)

        if strings.Contains(d, input) {
            same  = d
            same  = ","
            fmt.Println(d)
            fmt.Println(same)
        }
    }
    return ""
}

func main() {
    fmt.Println(FindSimilarData("iphone", "laptop", "iphone 13", "iphone 12", "iphone 12 pro"))
}
  •  Tags:  
  • go
  • Related