Change hex to string - go

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

Related

find number in string and replace it with a string from a dictionary in golang

I am new to Golang; but I have a project that require me to work with Golang.
My Problem: I have an output coming from a variable and that output contains some numbers. those number represent a named instance.
What I am trying to do: To check the output (alertmap) for number and check the number (num_var) found against a dictionary I have created. If a match is found; replace the number (num_var) with the string (value)from the dictionary in the (alertmap).
I have a variable called alertmap that has a key and a value.
The key is what I want to change
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"os"
"strconv"
"time"
"errors"
"regexp"
"strings"
)
my_dicts = {
123456: "project_data"
2345671: "project_dev-1"
}
func alertme ()
for key, value := range alertmap
my_alert += fmt.Sprintln("\n", key, "is a", value.usage, "and Expires in:", value.expireComputed, "days")
}
what I want to do is insert an if statemnet after "for key, value := range alertmap" to check if key(my/123456/secret/salon for example) has a number in it then replace that number with the string that match from the dictionary. key will the be my/project_data/secret/salon (this is what I want sent as key).
Any help will be appreciated.
re := regexp.MustCompile("[0-9]+")
func alertme ()
for key, value := range alertmap {
my_alert += fmt.Sprintln("\n", key, "is a", value.usage, "and Expires in:", value.expireComputed, "days")
}
// Find value to replace and convert to int
dictKey, _ := strconv.Atoi(re.FindString(key))
// Replace value
replacedKey := re.ReplaceAllString(key, my_dicts[dictKey])
}

Golang: How to generate random float using only crypto/rand

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?

Why this repeats the same random number?

I'm new to Go and not sure why it prints the same number for rand.Intn(n int) int for every run:
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(rand.Intn(10))
}
The docs says :
Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.
And how do I properly seed the random number generation?
By calling the rand.Seed() function, passing it a (random) seed (typically the current unix timestamp). Quoting from math/rand package doc:
Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run. Use the Seed function to initialize the default Source if different behavior is required for each run.
Example:
rand.Seed(time.Now().UnixNano())
If rand.Seed() is not called, the generator behaves as if seeded by 1:
Seed uses the provided seed value to initialize the default Source to a deterministic state. If Seed is not called, the generator behaves as if seeded by Seed(1).
package main
import
(
"fmt"
"math/rand"
"time"
)
func randomGen(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max - min) + min
}
func main() {
randNum := randomGen(1, 10)
fmt.Println(randNum)
}
Under the package, math/rand, you can find a type Rand.
func New(src Source) *Rand - New returns a new Rand that uses random
values from src to generate other random values.
It actually needs a seed to generate it.
step 1: create a seed as a source using new Source.
time.Now().UnixNano() is used for the accuracy.
step 2: create a
type Rand from the seed
step 3: generate a random number.
Example:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
fmt.Println(r.Intn(100))
}

Random number issue in golang

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.

Why TrimLeft doesn't work as expected?

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

Resources