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
Related
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)
}
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.
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 2 years ago.
Improve this question
I want to check if time.Now is after another time.Time in Go.
person.CreatedAt is time.Time
if time.Now > person.CreatedAt {
fmt.Println("time.Now is after person.CreatedAt")
}
Here simple example how you can check it:
package main
import (
"fmt"
"time"
)
func main() {
dateFormat := "2006-01-02"
personCreatedAt, err := time.Parse(dateFormat, "2020-01-01")
if err != nil {
// error handling...
}
ok := time.Now().After(personCreatedAt)
fmt.Println(ok)
}
Result will be: true
You can use time.After, time.Before and time.Equal to compare times:
if time.Now().After(person.CreatedAt) {
fmt.Println("time.Now is after person.CreatedAt")
}
To check if a time.Time variable is empty use time.IsZero
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 2 years ago.
Improve this question
I'm learning Go and trying to use reference to integer value in if-clause.
package main
import (
"fmt"
)
func main() {
a := 19
b := &a
if b > 10 {
fmt.Println("Its bigger")
}
}
This gives error message for type mismath.
How could I successfully compare value which b is referencing. In my training code I'm reading command line arguments with flags, but I suppose this example is reprex.
How should I compare when havin only reference available?
Here b is a pointer of int means *int. You can't compare *int type with int type.
Use *b to dereference to get the value and then compare with constant value.
if *b > 10 {
fmt.Println("Its bigger")
}
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