A method in Golang to generate random strings and copy them to the paste board
- 2020-09-16 07:31:19
- OfStack
Some time ago in my life, I occasionally needed to rename some files, and it was a random name, just
It started with a manual renaming and typing a bunch of alphanumeric characters on the keyboard, but after a while it became a bit of a hassle, so I figured out if I could use golang to do this and automatically convert the generated strings
Copy to the paste board and generate the exe file with a double click. Just do it.
I did some research on the Internet and wrote about it.
Install the necessary libraries
go get github.com/atotto/clipboard
Code implementation
package main
import (
"fmt"
"github.com/atotto/clipboard"
"math/rand"
"strings"
"time"
)
var a = [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
func main() {
s := g()
fmt.Println(s)
// Copy contents to clipboard
clipboard.WriteAll(s)
}
func g() string {
var builder strings.Builder
for i := 0; i < 10; i++ {
rand.Seed(time.Now().UnixNano())
time.Sleep(1 * time.Nanosecond)
c := rand.Intn(36)
builder.WriteString(a[c])
}
return builder.String()
}
I'm just randomly generating 10 characters here, but you can change it yourself.
Finally, the exe file is generated. When you want to use it, double-click 1 and you have it in your paste board.