当前位置: 首页>>代码示例>>Golang>>正文


Golang to.String函数代码示例

本文整理汇总了Golang中menteslibres/net/gosexy/to.String函数的典型用法代码示例。如果您正苦于以下问题:Golang String函数的具体用法?Golang String怎么用?Golang String使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了String函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestGet

func TestGet(t *testing.T) {

	settings, err := Open("_examples/input/settings.yaml")

	if err != nil {
		t.Errorf("Test failed.")
	}

	test1 := "Hello World!"
	val1 := to.String(settings.Get("test_string"))

	if val1 != test1 {
		t.Errorf("Got %t expecting %t.", val1, test1)
	}

	val2 := int(to.Int64(settings.Get("non_defined_int")))

	if val2 != 0 {
		t.Errorf("Test failed.")
	}

	test3 := "Third"

	val3 := settings.Get("test_map", "element_3", "test_sequence").([]interface{})

	if val3[2] != test3 {
		t.Errorf("Got %t expecting %t.", val3[2], test3)
	}

	test5 := "Hello World!"
	val5 := to.String(settings.Get("test_string"))

	if test5 != val5 {
		t.Errorf("Got %t expecting %t.", test5, val5)
	}

	test6 := 1234
	val6 := int(to.Int64(settings.Get("test_int")))

	if test6 != val6 {
		t.Errorf("Got %t expecting %t.", test6, val6)
	}

	test7 := float64(1.2)
	val7 := to.Float64(settings.Get("test_float"))

	if test7 != val7 {
		t.Errorf("Got %t expecting %t.", test7, val7)
	}

	test8 := true
	val8 := to.Bool(settings.Get("test_bool"))

	if test8 != val8 {
		t.Errorf("Got %t expecting %t.", test8, val8)
	}

}
开发者ID:javadabadoo,项目名称:yaml,代码行数:58,代码来源:yaml_test.go

示例2: loadSettings

// Loads settings
func loadSettings(file string) (*yaml.Yaml, error) {

	var entries map[interface{}]interface{}
	var ok bool

	// Trying to read settings from file.
	y, err := yaml.Open(file)

	if err != nil {
		return nil, err
	}

	// Loading and verifying host entries
	if entries, ok = y.Get("hosts").(map[interface{}]interface{}); ok == false {
		return nil, errors.New("Missing \"hosts\" entry.")
	}

	h := map[string]*host.Host{}

	// Populating host entries.
	for key := range entries {
		name := to.String(key)
		path := to.String(entries[name])

		info, err := os.Stat(path)
		if err != nil {
			return nil, fmt.Errorf("Failed to validate host %s: %q.", name, err)
		}
		if info.IsDir() == false {
			return nil, fmt.Errorf("Host %s does not point to a directory.", name)
		}

		h[name], err = host.New(name, path)

		if err != nil {
			return nil, fmt.Errorf("Failed to initialize host %s: %q.", name, err)
		}
	}

	for name := range hosts {
		hosts[name].Close()
	}

	hosts = h

	if _, ok := hosts["default"]; ok == false {
		log.Printf("Warning: default host was not provided.\n")
	}

	return y, nil
}
开发者ID:xiam,项目名称:luminos,代码行数:52,代码来源:server.go

示例3: Append

/*
	Appends items into the table. An item could be either a map or a struct.
*/
func (self *Table) Append(items ...interface{}) ([]db.Id, error) {

	ids := make([]db.Id, len(items))

	for i, item := range items {

		fields, values, err := self.FieldValues(item, toInternal)

		// Error ocurred, stop appending.
		if err != nil {
			return ids, err
		}

		res, err := self.source.doExec(
			fmt.Sprintf("INSERT INTO `%s`", self.Name()),
			sqlFields(fields),
			"VALUES",
			sqlValues(values),
		)

		// Error ocurred, stop appending.
		if err != nil {
			return ids, err
		}

		// Last inserted ID could be zero too.
		id, _ := res.LastInsertId()
		ids[i] = db.Id(to.String(id))
	}

	return ids, nil
}
开发者ID:jurka,项目名称:db,代码行数:35,代码来源:collection.go

示例4: getJSONList

func (a *ABF) getJSONList(id uint64) (list map[string]interface{}, err error) {
	req, err := http.NewRequest("GET", abfURL+"/api/v1/build_lists/"+to.String(id)+".json", nil)
	if err != nil {
		return
	}

	req.SetBasicAuth(abfUser, abfAPIKey)

	resp, err := abfClient.Do(req)
	if err != nil {
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var result map[string]interface{}

	err = json.Unmarshal(body, &result)
	if err != nil {
		return
	}

	dig.Get(&result, &list, "build_list")
	return
}
开发者ID:robxu9,项目名称:kahinah,代码行数:29,代码来源:abf.go

示例5: makeBuildList

func (a *ABF) makeBuildList(list map[string]interface{}) (*models.List, error) {
	pkg := a.makePkgList(list)

	changelog := ""

	var logs []interface{}
	dig.Get(&list, &logs, "logs")

	for _, v := range logs {
		asserted := v.(map[string]interface{})
		if dig.String(&asserted, "file_name") == "changelog.log" {
			changelog = dig.String(&asserted, "url")
			break
		}
	}

	handleID := to.String(dig.Uint64(&list, "id"))
	handleProject := dig.String(&list, "project", "fullname")
	platform := dig.String(&list, "save_to_repository", "platform", "name")

	bl := models.List{
		Name:     dig.String(&list, "project", "name"),
		Platform: platform,
		Channel:  dig.String(&list, "save_to_repository", "name"),
		Variants: dig.String(&list, "arch", "name"),

		Artifacts: pkg,
		Links: []models.ListLink{
			{
				Name: models.LinkMain,
				URL:  fmt.Sprintf("%s/build_lists/%v", abfURL, handleID),
			},
			{
				Name: models.LinkChangelog,
				URL:  changelog,
			},
			{
				Name: models.LinkSCM,
				URL:  fmt.Sprintf("%s/%v/commits/%v", abfURL, handleProject, platform),
			},
		},
		Changes:   changelog,
		BuildDate: time.Unix(dig.Int64(&list, "updated_at"), 0),

		StageCurrent: models.ListStageNotStarted,
		StageResult:  models.ListRunning,
		Activity: []models.ListActivity{
			{
				UserID:   models.FindUser(models.UserSystem).ID,
				Activity: "Imported this build list from ABF.",
			},
		},

		IntegrationName: "abf",
		IntegrationOne:  "[" + handleID + "]",
		IntegrationTwo:  dig.String(&list, "commit_hash"),
	}

	return &bl, nil
}
开发者ID:robxu9,项目名称:kahinah,代码行数:60,代码来源:abf.go

示例6: toInternal

// Converts a Go value into internal database representation.
func toInternal(val interface{}) interface{} {
	switch t := val.(type) {
	case db.Marshaler:
		return t
	case []byte:
		return string(t)
	case *time.Time:
		if t == nil || t.IsZero() {
			return sqlgen.Value{sqlgen.Raw{sqlNull}}
		}
		return t.Format(DateFormat)
	case time.Time:
		if t.IsZero() {
			return sqlgen.Value{sqlgen.Raw{sqlNull}}
		}
		return t.Format(DateFormat)
	case time.Duration:
		return fmt.Sprintf(TimeFormat, int(t/time.Hour), int(t/time.Minute%60), int(t/time.Second%60), t%time.Second/time.Millisecond)
	case sql.NullBool:
		if t.Valid {
			if t.Bool {
				return toInternal(t.Bool)
			}
			return false
		}
		return sqlgen.Value{sqlgen.Raw{sqlNull}}
	case sql.NullFloat64:
		if t.Valid {
			if t.Float64 != 0.0 {
				return toInternal(t.Float64)
			}
			return float64(0)
		}
		return sqlgen.Value{sqlgen.Raw{sqlNull}}
	case sql.NullInt64:
		if t.Valid {
			if t.Int64 != 0 {
				return toInternal(t.Int64)
			}
			return 0
		}
		return sqlgen.Value{sqlgen.Raw{sqlNull}}
	case sql.NullString:
		if t.Valid {
			return toInternal(t.String)
		}
		return sqlgen.Value{sqlgen.Raw{sqlNull}}
	case bool:
		if t == true {
			return `1`
		}
		return `0`
	}

	return to.String(val)
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:57,代码来源:collection.go

示例7: ActivityJSONHandler

func ActivityJSONHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	dataRenderer := data.FromContext(ctx)

	page := int(to.Int64(r.FormValue("page")))
	if page <= 0 {
		page = 1
	}

	limit := int(to.Int64(r.FormValue("limit")))
	if limit <= 0 {
		limit = 50
	}

	var cnt int
	if err := models.DB.Model(&models.ListActivity{}).Count(&cnt).Error; err != nil {
		panic(err)
	}

	totalpages := cnt / 50
	if cnt%50 != 0 {
		totalpages++
	}

	if page > totalpages {
		page = totalpages
	}

	var activities []models.ListActivity
	if err := models.DB.Limit(limit).Offset((page - 1) * limit).Order("created_at desc").Find(&activities).Error; err != nil && err != gorm.ErrRecordNotFound {
		panic(err)
	}

	// render a better karma view
	var rendered []*activityJSON
	for _, v := range activities {
		// load the username...
		rendered = append(rendered, &activityJSON{
			ListId:  v.ListID,
			User:    models.FindUserByID(v.UserID).Username,
			Comment: string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(v.Activity)))),
			Time:    v.CreatedAt,
			URL:     render.ConvertURL("/b/" + to.String(v.ListID)),
		})
	}

	dataRenderer.Data = map[string]interface{}{
		"totalpages": totalpages,
		"page":       page,
		"activities": rendered,
	}
	dataRenderer.Type = data.DataJSON
}
开发者ID:robxu9,项目名称:kahinah,代码行数:52,代码来源:activity.go

示例8: BuildGetHandler

// BuildGetHandler displays build information for a specific build
func BuildGetHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	dataRenderer := data.FromContext(ctx)

	toRender := map[string]interface{}{}

	id := to.Uint64(pat.Param(ctx, "id"))

	var pkg models.List
	if err := models.DB.Where("id = ?", id).First(&pkg).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			panic(ErrNotFound)
		} else {
			panic(err)
		}
	}

	toRender["Title"] = "Build " + to.String(id) + ": " + pkg.Name
	toRender["Nav"] = 2
	toRender["ID"] = to.String(id)

	dataRenderer.Data = toRender
	dataRenderer.Template = "builds/build"
}
开发者ID:robxu9,项目名称:kahinah,代码行数:24,代码来源:builds.go

示例9: loadTemplates

// Loads templates with .tpl extension from the templates directory. At this
// moment only index.tpl is expected.
func (host *Host) loadTemplates() error {

	tpldir := to.String(host.Settings.Get("document", "templates"))

	if tpldir == "" {
		tpldir = "templates"
	}

	tplroot := host.DocumentRoot + PS + tpldir

	fp, err := os.Open(tplroot)

	if err != nil {
		return fmt.Errorf("Error trying to open %s: %s", tplroot, err.Error())
	}

	host.TemplateRoot = tplroot

	defer fp.Close()

	files, err := fp.Readdir(-1)

	if err != nil {
		return fmt.Errorf("Error reading directory %s: %s", tplroot, err.Error())
	}

	for _, fp := range files {

		if strings.HasSuffix(fp.Name(), ".tpl") == true {

			file := host.TemplateRoot + PS + fp.Name()

			err := host.loadTemplate(file)

			if err != nil {
				log.Printf("%s: Template error in file %s: %s\n", host.Name, file, err.Error())
			}

		}
	}

	if _, ok := host.Templates["index.tpl"]; ok == false {
		return fmt.Errorf("Template %s could not be found.", "index.tpl")
	}

	return nil

}
开发者ID:reedobrien,项目名称:luminos,代码行数:50,代码来源:main.go

示例10: loadTemplates

// loadTemplates loads templates with .tpl extension from the templates
// directory. At this moment only index.tpl is expected.
func (host *Host) loadTemplates() error {
	var err error
	var fp *os.File

	tpldir := to.String(host.Settings.Get("content", "templates"))

	if tpldir == "" {
		tpldir = "templates"
	}

	tplroot := host.DocumentRoot + pathSeparator + tpldir

	if fp, err = os.Open(tplroot); err != nil {
		return fmt.Errorf("Error trying to open %s: %q", tplroot, err)
	}

	defer fp.Close()

	host.TemplateRoot = tplroot

	var files []os.FileInfo
	if files, err = fp.Readdir(-1); err != nil {
		return fmt.Errorf("Error reading directory %s: %q", tplroot, err)
	}

	for _, fp := range files {

		if strings.HasSuffix(fp.Name(), ".tpl") == true {

			file := host.TemplateRoot + pathSeparator + fp.Name()

			err := host.loadTemplate(file)

			if err != nil {
				log.Printf("%s: Template error in file %s: %q\n", host.Name, file, err)
			}

		}
	}

	if _, ok := host.Templates["index.tpl"]; ok == false {
		return fmt.Errorf("Template %s could not be found.", "index.tpl")
	}

	return nil

}
开发者ID:xiam,项目名称:luminos,代码行数:49,代码来源:main.go

示例11: toInternal

// Converts a Go value into internal database representation.
func toInternal(val interface{}) interface{} {
	switch t := val.(type) {
	case []byte:
		return string(t)
	case time.Time:
		return t.Format(DateFormat)
	case time.Duration:
		return fmt.Sprintf(TimeFormat, int(t/time.Hour), int(t/time.Minute%60), int(t/time.Second%60), t%time.Second/time.Millisecond)
	case bool:
		if t == true {
			return `1`
		} else {
			return `0`
		}
	}
	return to.String(val)
}
开发者ID:vimdude,项目名称:db,代码行数:18,代码来源:collection.go

示例12: toInternal

/*
	Converts a Go value into internal database representation.
*/
func toInternal(val interface{}) string {

	switch t := val.(type) {
	case []byte:
		return string(t)
	case time.Time:
		return t.Format(DateFormat)
	case time.Duration:
		return fmt.Sprintf(TimeFormat, int(t.Hours()), int(t.Minutes())%60, int(t.Seconds())%60, uint64(t.Nanoseconds())%1e9)
	case bool:
		if t == true {
			return "1"
		} else {
			return "0"
		}
	}

	return to.String(val)
}
开发者ID:jurka,项目名称:db,代码行数:22,代码来源:collection.go

示例13: Accept

// Accept tells ABF to publish the build lists specified in the List.
func (a *ABF) Accept(m *models.List) error {
	for _, v := range strings.Split(m.IntegrationOne, ";") {
		noBrackets := v[1 : len(v)-1]
		id := to.Uint64(noBrackets)
		req, err := http.NewRequest("PUT", abfURL+"/api/v1/build_lists/"+to.String(id)+"/publish.json", nil)
		if err != nil {
			return err
		}

		req.SetBasicAuth(abfUser, abfAPIKey)
		req.Header.Add("Content-Length", "0")

		resp, err := abfClient.Do(req)
		if err != nil {
			return err
		}

		defer resp.Body.Close()
	}
	return nil
}
开发者ID:robxu9,项目名称:kahinah,代码行数:22,代码来源:abf.go

示例14: sendToTesting

func (a *ABF) sendToTesting(id uint64) error {
	req, err := http.NewRequest("PUT", abfURL+"/api/v1/build_lists/"+to.String(id)+"/publish_into_testing.json", nil)
	if err != nil {
		return err
	}

	req.SetBasicAuth(abfUser, abfAPIKey)
	req.Header.Add("Content-Length", "0")

	resp, err := abfClient.Do(req)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to send %d to testing: %s\n", id, err.Error())
		return err
	}

	defer resp.Body.Close()
	// we are not doing this again...
	//bte, _ := ioutil.ReadAll(resp.Body)
	//fmt.Printf("sending %d to testing yielded %s\n", id, bte)
	return nil
}
开发者ID:robxu9,项目名称:kahinah,代码行数:21,代码来源:abf.go

示例15: getContentPath

func (host *Host) getContentPath() (string, error) {
	var directories []string

	contentdir := to.String(host.Settings.Get("content", "markdown"))
	if contentdir == "" {
		directories = []string{
			"content",
			"markdown",
		}
	} else {
		directories = []string{contentdir}
	}

	for _, directory := range directories {
		path := host.DocumentRoot + pathSeparator + directory
		if _, err := os.Stat(path); err == nil {
			return path, nil
		}
	}

	return "", errors.New(`Content directory was not found.`)
}
开发者ID:xiam,项目名称:luminos,代码行数:22,代码来源:main.go


注:本文中的menteslibres/net/gosexy/to.String函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。