How to store dynamic input in maps [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 3 years ago.
Improve this question
In Go, how should I put dynamic/user input in maps
func main() {
mapp := make(map[int]string)
for i := 0; i < 5; i++ {
fmt.Scanf("%d %s", &mapp[i], &mapp[i])
}
fmt.Print(mapp)
}

You can't create a key-value pair on a map like that.
What you can do, is store the input in variables, and then create a map key-value pair with the input. Like this:
for i := 0 ; i < 5 ; i++ {
var num int
var text string
fmt.Scanf("%d %s",&num,&text)
mapp[num] = text
}

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 can I assign a function to a variable without executing it? [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
When I try to assign a map key to a function it executes the function. How can I do this without executing the function?
var myslice []int
myslice = append(myslice, 1)
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
a["removeIndex"] = removeIndex(myslice, 0)
Just assign the function to the map[string]func([]string,int)[]string's key
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
a["removeIndex"] = RemoveIndex
rifn := a["removeIndex"]
rifn(myslice, 0)

is access to single variable from routines integrity safe? [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
Given such a code:
type Test struct {
V int
}
func (t *Test) t() {
t.V += 1
}
func main() {
t := Test{}
for i := 1; i <= 1000000; i++ {
go t.t()
}
for {
fmt.Println(t.V)
time.Sleep(3 * time.Second)
}
}
I hope it is clear, why do I do this test. And the result is never 1000000. It always prints number less than 1000000.. So, only go channels can I use to solve it ?
Why don't I have 1000000 in result ? How to get 1000000 ?
is access to single variable from routines integrity safe?
No. Everything that is racy is not safe. Under no circumstances. Not if is a single variable, not if accessed by an odd number of goroutines and not on Mondays. Never.

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