init module

This commit is contained in:
Nam Huynh 2021-08-09 10:12:08 +07:00
parent 41a804eac9
commit b15cf7a95b
7 changed files with 77 additions and 0 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.idea

10
get_float64.go Normal file
View File

@ -0,0 +1,10 @@
package zookeeper
import "strconv"
// GetFloat64Value ...
func GetFloat64Value(path string) float64 {
s := GetStringValue(path)
v, _ := strconv.ParseFloat(s, 64)
return v
}

10
get_int.go Normal file
View File

@ -0,0 +1,10 @@
package zookeeper
import "strconv"
// GetIntValue ...
func GetIntValue(path string) int {
s := GetStringValue(path)
v, _ := strconv.Atoi(s)
return v
}

12
get_string.go Normal file
View File

@ -0,0 +1,12 @@
package zookeeper
import "fmt"
// GetStringValue ...
func GetStringValue(path string) string {
value, _, err := zkClient.Get(path)
if err != nil {
fmt.Printf("Get value from path %s err: %v\n", path, err)
}
return string(value)
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module github.com/Selly-Modules/zookeeper
go 1.16
require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414
)

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 h1:AJNDS0kP60X8wwWFvbLPwDuojxubj9pbfK7pjHw0vKg=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=

31
zookeeper.go Normal file
View File

@ -0,0 +1,31 @@
package zookeeper
import (
"fmt"
"time"
"github.com/logrusorgru/aurora"
"github.com/samuel/go-zookeeper/zk"
)
var zkClient *zk.Conn
// Connect ...
func Connect(uri string) error {
c, _, err := zk.Connect([]string{uri}, time.Second*30)
if err != nil {
fmt.Println("Error when connect to Zookeeper", uri, err)
return err
}
fmt.Println(aurora.Green("*** CONNECTED TO ZOOKEEPER: " + uri))
// Set client
zkClient = c
return nil
}