< operator in Go lang [closed] - go

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
Well I am a newby in Go lang, but this doesn't make sense to me:
package main
import (
"fmt"
"log"
)
var rectLen, rectWidth float64 = 0, 0
func init() {
fmt.Println("init is initialized")
if rectLen < 0 {
log.Fatal("rectLen smaller than 0")
}
if rectWidth < 0 {
log.Fatal("rectWidht smaller than 0")
}
}
func main() {
fmt.Println("Main is initialized")
fmt.Println(rectLen, rectWidth )
}
This will print out:
init is initialized
Main is initialized
0 0
Why is 0 and 0 printed out when my init function is "guarding" that my rectLen, rectWidth variables should be strictly greater than 0?
If I change the values to something less than 0, it works fine, I get:
init is initialized
2009/11/10 23:00:00 rectLen smaller than 0
Thanks!

Because < is not the same as “equal to”. Try changing your operators to <=. This should fire only if your value is less than OR equal to 0

Related

How to check if int flag is set [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 4 months ago.
Improve this question
How to check if pointer int flag is set?
package main
import (
"flag"
"log"
)
var verbose *int
func main(){
verbose = flag.Int("v", 0, "verbosity")
if verbose != 0 {
log.Print("I'm verbose")
}
}
error
cannot convert 0 (untyped int constant) to *int
You're missing a couple of things:
func main() {
verbose = flag.Int("v", 0, "verbosity")
flag.Parse() // <- this
// and dereferencing the *int pointer
if *verbose != 0 {
log.Print("I'm verbose")
}
}
From the flag.Parse docs:
... parses the command-line flags from os.Args[1:]. Must be called
after all flags are defined and before flags are accessed by the
program.
Output:
$ ./ff -v 2
2022/10/11 18:53:18 I'm verbose

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

Ranging over golang map, inserting the new key [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 12 months ago.
Improve this question
func hello() {
myMap := make(map[int]bool)
i := 0
myMap[0] = false
for val, ok := myMap[i]; ok && !val; {
fmt.Println("val", val)
i--
}
}
Why does this code run infinitely ? ideally it should once only once. can someone explain this ?
ok and val's are not update, this for update that values
for val, ok := myMap[i]; ok && !val; {
fmt.Println("val", val)
i--
val, ok = myMap[i]
}

How func ContainsAny() works in Go? [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 1 year ago.
Improve this question
Considering the following code
func main() {
arg := os.Args
if len(arg[1]) != 1 || len(arg) != 2 {
fmt.Println("Give me a letter.")
return
}
if (strings.IndexAny(arg[1], "yw") == 0) {
fmt.Printf("%q is a semivowel.\n", arg[1])
} else if strings.IndexAny(arg[1], "aeiou") == 0 {
fmt.Printf("%q is a vowel.\n", arg[1])
} else {
fmt.Printf("%q is a consonant.\n", arg[1])
}
}
and more specifically this section:
if (strings.IndexAny(arg[1], "yw") == 0) {
fmt.Printf("%q is a semivowel.\n", arg[1])
} else if strings.IndexAny(arg[1], "aeiou") == 0 {
fmt.Printf("%q is a vowel.\n", arg[1])
} else {
fmt.Printf("%q is a consonant.\n", arg[1])
}
I could not understand why it only worked when I informed the bool equal to zero but not when equal to one. (According to the official documentation https://pkg.go.dev/strings#ContainsAny shouldn't it be equal to 1, as in true?)
According to the documentation, the function you used should return an integer:
package strings // import "strings"
func IndexAny(s, chars string) int
IndexAny returns the index of the first instance of any Unicode code point
from chars in s, or -1 if no Unicode code point from chars is present in s.
and strings.ContainsAny returns a boolean
package strings // import "strings"
func ContainsAny(s, chars string) bool
ContainsAny reports whether any Unicode code points in chars are within s.
You can read the documentation from a terminal running the command:
go doc strings.IndexAny # or any go function or package you want

missing go type for returned values [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 1 year ago.
Improve this question
How come no warnings are issued for the following code?
$ cat ret.go
package main
import "fmt"
func foobar(x int, y int) (z, w int) {
if x+y > 100 {
_,w = 3,5
} else {
_,w = "MMM",9
}
return z,w
}
func main() {
var x int
_,x = foobar(42,13)
fmt.Println(x)
}
$ go build -gcflags=-l ret.go
For the least, the go compiler should know the size of z right?
In golang, you can define multiple variable in one line like next:
var identifier1, identifier2 type
So, z, w here both declared as int.
Additional, see this:
The return values of a function can be named in Golang
Then, if you not assign a value to z, it will has a default value of int, that is 0. So, no warning, the code is ok.

Resources