Golang use LIKE inside IF statement best practice [closed] - go

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
if i have many option to be filter in IF statement, can i use some function like this?
s1 := "lorem ipsum"
sum := 0
for _, v := range s1 {
if string(v) like ("a","i","u","e","o") {
sum+=1
}
}

You can use switch statement.
Switch supports multiple matches
switch string(v) {
case "a","i","u","e","o":
sum+=1
}
Tried it in the playground - https://play.golang.org/p/QRYhEDA7EUZ

Related

Golang Text representation as decimal string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
How to convert "Hello" to string "72101108108111"
[]byte("Hello")=> [72 101 108 108 111]
byteStr := []byte("Hello")
var result string
for _, i := range byteStr {
result += fmt.Sprintf("%d", i)
}
fmt.Println(result)

convert string of array to array of string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a raw string which is formed from array of strings like the one defined below
"['a','b','c']"
which i want to convert to proper array of string as below
["a","b","c"]
I am trying this on golang. However, i didn't succeed in getting this done. How this can be accomplished in go
var s = "['a','b','c']"
ss := strings.Split(strings.Trim(s, "[]"), ",")
a := make([]string, len(ss))
for i := range ss {
a[i] = strings.Trim(ss[i], "'")
}
out, err := json.Marshal(a)
if err != nil {
panic(err)
}
fmt.Println(string(out))
// output: ["a","b","c"]
https://go.dev/play/p/kQ0Up06K9zz

how to parse and get url from a common string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to parse and get url from a string such as hello google.comxxx,the output should be google.com, is there any easy way to do that?
There's a package named xurls which you can use.
import "mvdan.cc/xurls/v2"
func main() {
rxRelaxed := xurls.Relaxed()
rxRelaxed.FindString("Do gophers live in golang.org?") // "golang.org"
rxRelaxed.FindString("This string does not have a URL") // ""
rxStrict := xurls.Strict()
rxStrict.FindAllString("must have scheme: http://example.com/.", -1) // []string{"http://example.com/"}
rxStrict.FindAllString("no scheme, no match: example.com", -1) // []string{}
}

How to check words with the same characters where the words in one variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm thinking up about how I find the same characters in one variable looks like this:
var words string = "abab"
and then I want's to eliminate the same characters in that one variable and here's the output to be
Output:
ab
have any solution about this?
One solution can be the use of go map[] to track the taken characters.
sample code:
func main() {
s := "abcdaabcefgahccij"
newS := ""
taken := make(map[rune]int)
for _, value := range s {
if _, ok := taken[value]; !ok {
taken[value] = 1
newS += string(value)
}
}
fmt.Println(newS)
}
Output:
abcdefghij

How do I break out of an infinite loop in Golang [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm making a program in Go for guessing a random number. I'm having issues with a for loop.
I can't stop the for loop from continuously iterating.
How do i break out of the loop once a condition is satisfied.
for loop == true {
//fmt.Println("read number", i, "/n")
if i == ans {
fmt.Println("well done")
}
if i != ans {
fmt.Println("Nope")
fmt.Scan(i)
}
You need to break out of the loop:
for {
fmt.Scan(i)
if i == ans {
fmt.Println("well done")
break
}
if i != ans {
fmt.Println("Nope")
}
}

Resources