You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
643 B
36 lines
643 B
2 months ago
|
package snowflake
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
sf "github.com/bwmarrin/snowflake"
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
var node *sf.Node
|
||
|
|
||
|
func Init() (err error) {
|
||
|
startTime := viper.GetString("app.start_time")
|
||
|
machineID := viper.GetInt64("machine_id")
|
||
|
var st time.Time
|
||
|
st, err = time.Parse("2006-01-02", startTime)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
sf.Epoch = st.UnixNano() / 1000000
|
||
|
node, err = sf.NewNode(machineID)
|
||
|
return
|
||
|
}
|
||
|
func GenID() int64 {
|
||
|
return node.Generate().Int64()
|
||
|
}
|
||
|
|
||
|
// func main() {
|
||
|
// if err := Init("2020-07-01", 1); err != nil {
|
||
|
// fmt.Printf("init failed, err:%v\n", err)
|
||
|
// return
|
||
|
// }
|
||
|
// id := GenID()
|
||
|
// fmt.Println(id)
|
||
|
// }
|