The math library doesn't seem to have a Min function for float64. How would I get the smallest non-zero float64?
as #oliver-charlesworth says:
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%.12G", math.SmallestNonzeroFloat64)
}
https://play.golang.org/p/kRuIhalODGa
Output:
4.94065645841E-324
Related
I'm using gosec to check if my code has any security flaws. But it is reporting the use of math/rand (Use of weak random number generator (math/rand instead of crypto/rand)) pkg in this code:
package main
import (
"fmt"
"math/rand"
)
func main() {
a := rand.Float64()
fmt.Println(a)
}
The problem is: crypto/rand does not have the option to get a random float: https://pkg.go.dev/crypto/rand
How can I do that?
This is the code I'm working with:
package main
import "fmt"
import "math/rand"
func main() {
code := rand.Intn(900000)
fmt.Println(code)
}
It always returns 698081. I don't understand, what the is problem?
https://play.golang.org/p/XisNbqCZls
Edit:
I tried rand.Seed
package main
import "fmt"
import "time"
import "math/rand"
func main() {
rand.Seed(time.Now().UnixNano())
code := rand.Intn(900000)
fmt.Println(code)
}
There is no change. Now it always returns 452000
https://play.golang.org/p/E_Wfm5tOdH
https://play.golang.org/p/aVWIN1Eb84
A couple of reasons why you'll see the same result in the playground
Golang playground will cache the results
The time in the playground always starts at the same time to make the playground deterministic.
Last but not least, the rand package default seed is 1 which will make the result deterministic. If you place a rand.Seed(time.Now().UnixNano()) you'll receive different results at each execution. Note that this won't work on the playground for the second reason above.
I'm going through the Golang tutorial, and I am on this part
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Seed)
}
This returns My favorite number is 0xb1c20
I have been reading on https://golang.org/pkg/math/rand/#Seed but I'm still a bit confused as to how have it instead of show the hex show a string
math/rand.Seed is a function; you are printing the function's location in memory. You probably meant to do something like the following:
package main
import (
"fmt"
"math/rand"
)
func main() {
rand.Seed(234) // replace with your seed value, or set the seed based off
// of the current time
fmt.Println("My favorite number is", rand.Int())
}
I've expected tag to be "account" but it is "ccount". Why is "a" removed?
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimLeft(s, "refs/tags")
fmt.Println(tag)
}
Run
Use TrimPrefix instead of TrimLeft
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimPrefix(s, "refs/tags/")
fmt.Println(tag)
}
Please notice that following TrimLeft calls will result the same "fghijk
" string:
package main
import (
"fmt"
"strings"
)
func main() {
s := "/abcde/fghijk"
tag := strings.TrimLeft(s, "/abcde")
fmt.Println(tag)
tag = strings.TrimLeft(s, "/edcba")
fmt.Println(tag)
}
So TrimLeft is not the method which fits your needs. I guess it's impossible to use it in the example you've given to get the result you expect.
It is working as documented:
TrimLeft returns a slice of the string s with all leading Unicode
code points contained in cutset removed
Because there's an 'a' in the first argument (the cutset) the leading 'a' in account is removed
Let's say I have a function that returns an interface{}. But I know that item returns is a slice of some kind. How can I determine the length of that slice? Here's sample code of what I tried, but they all cause compilation error.
package main
import (
"log"
"reflect"
)
func SomeKindOfSlice() interface{} {
return []int64{0,1,2,3,4,5,6,7,8,9}
}
func main() {
slice := SomeKindOfSlice()
/*log.Println(reflect.TypeOf(slice).Len())
log.Println(reflect.TypeOf(slice).Type().Len())
log.Println(reflect.ValueOf(slice).Type().Len())
log.Println(reflect.ValueOf(slice).Elem().Type().Len())
*/
log.Println(reflect.ValueOf(slice).Elem().Type().Len())
}
I'd like to avoid the brute force way of specifically type asserting the slice variable just to find the length.
In your current attempt of the refect package usage you are querying for the Len of the Type. So this assumes you are dealing with an array not a slice. The difference being that a array is a fixed size slice, a slice has unbound length.
Check this code for demonstration
package main
import (
"log"
"reflect"
)
func SomeKindOfSlice() interface{} {
return []int64{0,1,2,3,4,5,6,7,8,9}
}
func SomeKindOfArray() interface{} {
return [10]int64{0,1,2,3,4,5,6,7,8,9}
}
func main() {
log.Println(reflect.ValueOf(SomeKindOfSlice()).Len())
log.Println(reflect.ValueOf(SomeKindOfArray()).Type().Len())
}