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

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")
}
}

Related

How can I change the variable inside the if statement of switch statement in Golang? [closed]

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 6 months ago.
Improve this question
I am a newbie in Golang and now I have a requirement to change a value inside the if statement.
Here is my dummy code.
package main
func main() {
a := "hi"
pull_enable := true
switch a {
case "hi":
image_list := []float32{
0,
2,
}
for image:=0; image<len(image_list); image++{
if image == 0 {
pull_enable = true
break
}
}
}
}
I define a variable pull_enable outside of switch statement, and I want to change this variable value in the if statement, but when I built it, it encountered an issue below.
# command-line-arguments
pull_enable declared but not used
I am wondering how I can fix this issue. Is there any idea?
package main
import "fmt"
func main() {
a := "hi"
pullEnable := true
switch a {
case "hi":
image_list := []float32{
0,
2,
}
for image := 0; image < len(image_list); image++ {
if image == 0 {
pullEnable = true
break
}
}
}
fmt.Println(pullEnable)
}

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

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 store dynamic input in maps [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 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
}

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