Golang Text representation as decimal string [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 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)

Related

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

Golang use LIKE inside IF statement best practice [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 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

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{}
}

Golang how to convert type integer to type byte [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
How can I convert an integer 3 to byte '3'?
package main
import (
"fmt"
)
func main() {
num := 3
b := byte(num)
if b == '3' { fmt.Println("how to make this line true") }
fmt.Printf("%v, %T\n", b, b) // 3, uint8
fmt.Printf("%v, %T\n", '3', '3') // 51, int32
}
To do what you say you want to do, you could do this:
strconv.Itoa(3)[0] == '3'
https://play.golang.com/p/LFDDf1JfWDQ
However, as pointed out by others, this is probably not what you actually want to do.

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

Resources