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
Related
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)
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
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{}
}
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
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have written a golang program to generate the start date and end date of the month for different locations
if month != "" && year != "" {
var monthInt, _ = strconv.Atoi(month)
var yearInt, _ = strconv.Atoi(year)
timeZone, err := time.LoadLocation("America/Pheonix")
if err != nil {
fmt.Println(err)// nil
}
// currentLocation := time.Now().Location(timeZone) // when I use this it will works
// both are of same type
fmt.Println(reflect.TypeOf(timeZone))
fmt.Println(reflect.TypeOf(time.UTC))
firstOfMonth := time.Date(yearInt, time.Month(monthInt), 1, 0, 0, 0, 0, timeZone)
onlyStartDate := strings.Split(firstOfMonth.Format("2006-01-02 00:00:00 -0000"), " ")
lastOfMonth := firstOfMonth.AddDate(0, 1, -1).Format("2006-01-02 00:00:00 -0000")
onlyLastDate := strings.Split(lastOfMonth, " ")
merchantDb.GetProvidersOfTheMonth(onlyStartDate[0], onlyLastDate[0])
}
But when I run this code it will give me the error of the:-
time: missing Location in call to Date
Why this error is generating and how I will solve this error?
Please any suggestions!
It should be America/Phoenix, not America/Pheonix.
timeZone, _ := time.LoadLocation("America/Phoenix")
For more information about list of available time zones string, please see: https://golang.org/src/time/zoneinfo_abbrs_windows.go