本文整理匯總了Golang中github.com/eaciit/toolkit.Sprintf函數的典型用法代碼示例。如果您正苦於以下問題:Golang Sprintf函數的具體用法?Golang Sprintf怎麽用?Golang Sprintf使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Sprintf函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: StringValue
func StringValue(v interface{}, db string) string {
var ret string
switch v.(type) {
case string:
t, e := time.Parse(time.RFC3339, toolkit.ToString(v))
if e != nil {
ret = toolkit.Sprintf("%s", "'"+v.(string)+"'")
} else {
if strings.Contains(db, "oci8") {
// toolkit.Println(t.Format("2006-01-02 15:04:05"))
ret = "to_date('" + t.Format("02-01-2006 15:04:05") + "','DD-MM-YYYY hh24:mi:ss')"
} else {
ret = "'" + t.Format("2006-01-02 15:04:05") + "'"
}
}
case time.Time:
t := v.(time.Time).UTC()
if strings.Contains(db, "oci8") {
ret = "to_date('" + t.Format("2006-01-02 15:04:05") + "','yyyy-mm-dd hh24:mi:ss')"
} else {
ret = "'" + t.Format("2006-01-02 15:04:05") + "'"
}
case int, int32, int64, uint, uint32, uint64:
ret = toolkit.Sprintf("%d", v.(int))
case nil:
ret = ""
default:
ret = toolkit.Sprintf("%v", v)
}
return ret
}
示例2: TestInsert
func TestInsert(t *testing.T) {
t.Skip()
var e error
skipIfConnectionIsNil(t)
es := []string{}
qinsert := ctx.NewQuery().From(tableName).Insert()
for i := 1; i <= 3; i++ {
qty := toolkit.RandInt(10)
price := toolkit.RandInt(10) * 50
amount := qty * price
u := &Orders{
toolkit.Sprintf("ord0%d", i+10),
toolkit.Sprintf("item%d", i),
qty,
price,
amount,
toolkit.Sprintf("available"),
}
e = qinsert.Exec(toolkit.M{}.Set("data", u))
if e != nil {
es = append(es, toolkit.Sprintf("Insert fail %d: %s \n", i, e.Error()))
}
}
if len(es) > 0 {
t.Fatal(es)
}
operation = "Test Insert"
sintaks = `
ctx.NewQuery().From(tableName).Insert().
Exec(toolkit.M{}.Set("data", u))`
TestSelect(t)
}
示例3: genGo
func genGo(fi os.FileInfo, source, out string) error {
log.Info("Processing " + fi.Name())
fn := filepath.Join(source, fi.Name())
var (
bs []byte
e error
)
if bs, e = ioutil.ReadFile(fn); e != nil {
return errors.New("Open error " + e.Error())
}
pkg := new(PackageModel)
e = toolkit.UnjsonFromString(string(bs), pkg)
if e != nil {
return errors.New("Unmarshal JSON: " + e.Error())
}
for _, sm := range pkg.Structs {
e = sm.Write(pkg, out)
if e != nil {
return errors.New(toolkit.Sprintf("Write model %s: %s", sm.Name, e.Error()))
}
log.Info(toolkit.Sprintf("Writing %s.%s", pkg.Name, sm.Name))
}
log.Info("Processing " + fi.Name() + " done")
return nil
}
示例4: TestCRUD
func TestCRUD(t *testing.T) {
skipIfConnectionIsNil(t)
e := ctx.NewQuery().Delete().From(tableName).SetConfig("multiexec", true).Exec(nil)
if e != nil {
t.Fatalf("Delete fail: %s", e.Error())
}
es := []string{}
qinsert := ctx.NewQuery().From(tableName).SetConfig("multiexec", true).Insert()
for i := 1; i <= 50; i++ {
u := &testUser{
toolkit.Sprintf("user%d", i),
toolkit.Sprintf("User %d", i),
toolkit.RandInt(30) + 20, true}
e = qinsert.Exec(toolkit.M{}.Set("data", u))
if e != nil {
es = append(es, toolkit.Sprintf("Insert fail %d: %s \n", i, e.Error()))
}
}
if len(es) > 0 {
t.Fatal(es)
}
e = ctx.NewQuery().Update().From(tableName).Where(dbox.Lte("_id", "user2")).Exec(toolkit.M{}.Set("data", toolkit.M{}.Set("Enable", false)))
if e != nil {
t.Fatalf("Update fail: %s", e.Error())
}
}
示例5: DefineCommand
func (p *Page) DefineCommand(server *Server, sourceZipPath string, destZipPath string, appID string) (string, string, string, error) {
var ext string
if strings.Contains(server.CmdExtract, "7z") || strings.Contains(server.CmdExtract, "zip") {
ext = ".zip"
} else if strings.Contains(server.CmdExtract, "tar") {
ext = ".tar"
} else if strings.Contains(server.CmdExtract, "gz") {
ext = ".gz"
}
sourceZipFile := toolkit.Sprintf("%s%s", sourceZipPath, ext)
destZipFile := toolkit.Sprintf("%s%s", destZipPath, ext)
var unzipCmd string
// cmd /C 7z e -o %s -y %s
if server.ServerType == "windows" {
unzipCmd = toolkit.Sprintf("cmd /C %s", server.CmdExtract)
unzipCmd = strings.Replace(unzipCmd, `%1`, destZipPath, -1)
unzipCmd = strings.Replace(unzipCmd, `%2`, destZipFile, -1)
} else {
unzipCmd = strings.Replace(server.CmdExtract, `%1`, destZipFile, -1)
unzipCmd = strings.Replace(unzipCmd, `%2`, destZipPath, -1)
}
return unzipCmd, sourceZipFile, destZipFile, nil
}
示例6: doParseSize
func doParseSize(size float64, unit string) string {
if unit == "" {
unit = "B"
}
ret := ""
if size > 1024 {
size = size / 1024
if unit == "B" {
unit = "K"
} else if unit == "K" {
unit = "M"
} else if unit == "M" {
unit = "G"
} else if unit == "G" {
unit = "T"
} else {
unit = "P"
}
if unit != "P" {
ret = doParseSize(size, unit)
} else {
ret = toolkit.Sprintf("%2.2f%s", size, unit)
}
} else {
ret = toolkit.Sprintf("%2.2f%s", size, unit)
}
return ret
}
示例7: MatchV
func MatchV(v interface{}, f *Filter) bool {
match := false
/*
rv0 := reflect.ValueOf(v)
if rv0.Kind() == reflect.Ptr {
rv0 = reflect.Indirect(rv0)
}
rv1 := reflect.ValueOf(f.Value)
if rv1.Kind()==reflect.Ptr{
rv1=reflect.Indirect(rv1)
}
*/
//toolkit.Println("MatchV: ", f.Op, v, f.Value)
if toolkit.HasMember([]string{FilterOpEqual, FilterOpNoEqual, FilterOpGt, FilterOpGte, FilterOpLt, FilterOpLte}, f.Op) {
return toolkit.Compare(v, f.Value, f.Op)
} else if f.Op == FilterOpIn {
var values []interface{}
toolkit.FromBytes(toolkit.ToBytes(f.Value, ""), "", &values)
return toolkit.HasMember(values, v)
} else if f.Op == FilterOpNin {
var values []interface{}
toolkit.FromBytes(toolkit.ToBytes(f.Value, ""), "", &values)
return !toolkit.HasMember(values, v)
} else if f.Op == FilterOpContains {
var values []interface{}
var b bool
toolkit.FromBytes(toolkit.ToBytes(f.Value, ""), "", &values)
for _, val := range values {
// value := toolkit.Sprintf(".*%s.*", val.(string))
// b, _ = regexp.Match(value, []byte(v.(string)))
r := regexp.MustCompile(`(?i)` + val.(string))
b = r.Match([]byte(v.(string)))
if b {
return true
}
}
} else if f.Op == FilterOpStartWith || f.Op == FilterOpEndWith {
value := ""
if f.Op == FilterOpStartWith {
value = toolkit.Sprintf("^%s.*$", f.Value)
} else {
value = toolkit.Sprintf("^.*%s$", f.Value)
}
cond, _ := regexp.Match(value, []byte(v.(string)))
return cond
}
return match
}
示例8: TestSaveQuery
func TestSaveQuery(t *testing.T) {
var e error
for i := 1; i <= 5; i++ {
ds := new(colonycore.DataSource)
ds.ID = toolkit.Sprintf("ds%d", i)
ds.ConnectionID = "conn1"
ds.QueryInfo = toolkit.M{}
ds.MetaData = nil
e = colonycore.Save(ds)
if e != nil {
t.Fatalf("Save datasource fail. " + e.Error())
}
}
var dss []colonycore.DataSource
c, e := colonycore.Find(new(colonycore.DataSource), nil)
if e != nil {
t.Fatalf("Load ds fail: " + e.Error())
}
e = c.Fetch(&dss, 0, true)
if e != nil {
t.Fatalf("Ftech ds fail: " + e.Error())
}
if len(dss) != 5 {
t.Fatal("Fetch ds fail. Got %d records only", len(dss))
}
toolkit.Println("Data:", toolkit.JsonString(dss))
}
示例9: TestSaveApp
func TestSaveApp(t *testing.T) {
wd, _ := os.Getwd()
colonycore.ConfigPath = filepath.Join(wd, "../config")
for i := 1; i <= 5; i++ {
appn := new(colonycore.Application)
appn.ID = toolkit.Sprintf("appn%d", i)
appn.Enable = true
e = colonycore.Save(appn)
if e != nil {
t.Fatalf("Save %s fail: %s", appn.ID, e.Error())
}
}
appn := new(colonycore.Application)
e := colonycore.Get(appn, "appn5")
if e != nil {
t.Fatal(e)
}
appn.ID = "appn3"
e = colonycore.Delete(appn)
if e != nil {
t.Fatal(e)
}
}
示例10: TestStorageWrite
func TestStorageWrite(t *testing.T) {
skipIfClientNil(t)
es := []string{}
toolkit.Printf("Writing Data:\n")
for i := 0; i < 200; i++ {
dataku := toolkit.RandInt(1000)
totalInt += dataku
//toolkit.Printf("%d ", dataku)
in := toolkit.M{}.Set("key", fmt.Sprintf("public.dataku.%d", i)).Set("data", toolkit.ToBytes(dataku, ""))
writeResult := client.Call("set", in)
if writeResult.Status != toolkit.Status_OK {
es = append(es, toolkit.Sprintf("Fail to write data %d : %d => %s", i, dataku, writeResult.Message))
}
}
if len(es) > 0 {
errorTxt := ""
if len(es) <= 10 {
errorTxt = strings.Join(es, "\n")
} else {
errorTxt = strings.Join(es[:10], "\n") + "\n... And others ..."
}
t.Errorf("Write data fail.\n%s", errorTxt)
}
}
示例11: Set
func (s *SliceBase) Set(i int, d interface{}) error {
e := toolkit.SliceSetItem(s.data, i, d)
if e != nil {
return errors.New(toolkit.Sprintf("SliceBase.Set: [%d] %s", i, e.Error()))
}
return nil
}
示例12: getAvailableNode
func (c *Coordinator) getAvailableNode(data []byte) (nodeIndex int, e error) {
var currentMax float64
found := false
dataLength := float64(len(data))
nodes := c.Nodes(RoleStorage)
for k, n := range nodes {
resultAvail := n.Call("storagestatus", nil)
if resultAvail.Status == toolkit.Status_OK {
//m := toolkit.M{}
sm := struct {
Memory *StorageMedia
Physical *StorageMedia
}{}
resultAvail.GetFromBytes(&sm)
nodeAvailableSize := sm.Memory.Available()
if nodeAvailableSize > dataLength && nodeAvailableSize > currentMax {
found = true
currentMax = nodeAvailableSize
nodeIndex = k
}
}
}
if !found {
e = errors.New(toolkit.Sprintf("No node available to hosts %s bytes of data", ParseSize(dataLength)))
}
return
}
示例13: Write
/*
Write Write bytes of data into sebar storage.
- Data need to be defined as []byte on in["data"]
- To use memory or disk should be defined on in["storage"] as: MEM, DSK (sebar.StorageTypeMemory, sebar.StorageTypeMemory)
- If no in["storage"] or the value is not eq to either disk or memory, it will be defaulted to memory
*/
func (s *Storage) Write(in toolkit.M) *toolkit.Result {
r := toolkit.NewResult()
key := in.Get("key").(string)
storage := StorageTypeEnum(in.GetString("storage"))
if storage != StorageTypeMemory && storage != StorageTypeDisk {
storage = StorageTypeMemory
}
dataToWrite := in.Get("data").([]byte)
dataLen := len(dataToWrite)
// Validation
nodeCoordinator := s.NodeByID(s.Coordinator)
if nodeCoordinator == nil {
return r.SetErrorTxt(s.Address + " no Coordinator has been setup")
}
// Since all is ok commit the change
var ms *StorageMedia
if storage == StorageTypeMemory {
ms = s.MemoryStorage
} else {
ms = s.DiskStorage
}
ms.write(key, dataToWrite, nodeCoordinator)
s.Log.Info(toolkit.Sprintf("Writing %s (%s) to node %s", key, ParseSize(float64(dataLen)), s.Address))
return r
}
示例14: TestSave
func TestSave(t *testing.T) {
InitCall()
for i := 1; i <= 1000; i++ {
e := office.NewEmployee()
e.ID = "emp" + strconv.Itoa(i)
e.Title = toolkit.Sprintf("Test Title %d", i)
e.Address = toolkit.Sprintf("Address %d", i)
e.LastLogin = time.Now()
if math.Mod(float64(i), 2) == 0 {
e.Enable = true
} else {
e.Enable = false
}
//log.Printf("DB %+v", office.DB())
//log.Printf("e %+v", toolkit.JsonString(e))
office.DB().Save(e)
}
}
示例15: TestInsert
func TestInsert(t *testing.T) {
// t.Skip("Skip : Comment this line to do test")
skipIfConnectionIsNil(t)
es := []string{}
qinsert := ctx.NewQuery().From("Data_CUD").SetConfig("multiexec", true).Save()
for i := 1; i <= 5; i++ {
u := toolkit.M{}.Set("Id", toolkit.Sprintf("ID-1%d", i)).
Set("Email", toolkit.Sprintf("user-1%d", i)).
Set("FirstName", toolkit.Sprintf("User no.%d", i)).
Set("LastName", toolkit.Sprintf("Test no.%d", i))
e := qinsert.Exec(toolkit.M{}.Set("data", u))
if e != nil {
es = append(es, toolkit.Sprintf("Insert fail %d: %s \n", i, e.Error()))
}
}
if len(es) > 0 {
t.Fatal(es)
}
}