Variable capturing in string literal in Go?

huangapple go评论87阅读模式
英文:

Variable capturing in string literal in Go?

问题

在Ruby中,我可以像在bash中一样直接在字符串字面量中捕获变量。

SRCDIR = "aaa"
DSTDIR = "bbb"

puts "SRCDIR = #{SRCDIR}"
puts "DSTDIR = #{DSTDIR}"

这是一个简单而小巧的功能,但非常好用,可以让它感觉像一个shell脚本。如果我必须编写一个复杂的shell脚本,这个功能会帮助很多,因为它消除了替换、连接和格式表达式的需要。

Go语言有类似的功能吗?如果有的话,如何使用?

英文:

In Ruby, I could directly capture variables in string literals like in bash.

SRCDIR	=	"aaa"
DSTDIR	=	"bbb"

puts "SRCDIR = #{SRCDIR}"
puts "DSTDIR = #{DSTDIR}"

This is a simple and tiny feature, but very nice to make it feel like a shell script. If I have to write a complex shell script this helps a lot because this eliminates the need for substitution, concatenation and format expression.

Does Go have something like this? If it does, how to use it?

答案1

得分: 5

不需要格式化字符串是不行的;通常的做法是使用fmt.Printffmt.Sprintf

srcdir := "aaa"
dstdir := "bbb"

// 为了清晰起见,将Sprintf和Println分开
fmt.Println(fmt.Sprintf("SRCDIR = %s", srcdir))
fmt.Println(fmt.Sprintf("DSTDIR = %s", dstdir))

// 如果只是打印它们,可以缩短代码
fmt.Printf("SRCDIR = %s\n", srcdir)
fmt.Printf("DSTDIR = %s\n", dstdir)
英文:

Not without a formatting string; the usual way to do this is with fmt.Printf or fmt.Sprintf:

srcdir := "aaa"
dstdir := "bbb"

// separated out Sprintf and Println for clarity
fmt.Println(fmt.Sprintf("SRCDIR = %s", srcdir))
fmt.Println(fmt.Sprintf("DSTDIR = %s", dstdir))

// could be shortened if you're just printing them
fmt.Printf("SRCDIR = %s\n", srcdir)
fmt.Printf("DSTDIR = %s\n", dstdir)

答案2

得分: 1

你需要使用+运算符进行连接,就像在JavaScript中一样。

main.go

package main

import "fmt"

func main() {

	movieQuote := `"What's the most you ever lost on a coin toss?"`
	statement := `反引号允许双引号,` + movieQuote + `,以及单引号`

	fmt.Println("movieQuote: ", movieQuote)
	fmt.Println("statement: ", statement)
}

运行

go run main.go

输出:

movieQuote:  "What's the most you ever lost on a coin toss?"
statement:  反引号允许双引号,"What's the most you ever lost on a coin toss?",以及单引号
英文:

You have to concat with the + operator as in JS

main.go

package main

import "fmt"

func main() {

	movieQuote := `"What's the most you ever lost on a coin toss?"`
	statement := `Back-ticks allow double quotes, ` + movieQuote + `, and single quote apostrophe's`

	fmt.Println("movieQuote: ", movieQuote)
	fmt.Println("statement: ", statement)
}

Run

go run main.go

Output:

movieQuote:  "What's the most you ever lost on a coin toss?"
statement:  Back-ticks allow double quotes, "What's the most you ever lost on a coin toss?", and single quote apostrophe's

答案3

得分: 0

Wes说的没错。我应该补充一下,如果你正在使用自定义类型,你可以为它们定义一个方法,方法的签名为String() string(实际上是为了使它们满足fmt.Stringer接口),然后直接将这些类型的实例传递给fmt包中期望一个字符串的函数,比如fmt.Println()。关于这个的简单介绍可以在《Effective Go》中找到(链接)。

英文:

What Wes said. I should add that if you're using custom types, you might define a method which has the signature String() string on them (to essentially make them satisfy the fmt.Stringer interface) and then pass instances of these types directly to functions of the fmt package which expect a string, such as fmt.Println(). A simple introduction to this might be found in "Effective Go".

答案4

得分: 0

GQL查询

package main

import (
	"github.com/gookit/color"
)

const (
	offerInfo string = `{
	    id
	    name
	    description
	    logoURL
	    opt_ins {
	      id
	      name
	      description
	    }
	  }`
)

func QueryAllOffers() string {
	return `{ offer ` + offerInfo + `}`
}

func QueryOfferByID(id string) string {
	return `{
		offer (id: "` + string(id)  + `")` + offerInfo + ` }`
}

func main() {
	queryAllOffers := QueryAllOffers()
	color.Cyan.Println(queryAllOffers)

	offerID := "0001"
	queryOfferByID := QueryOfferByID(offerID)
	color.Blue.Println(queryOfferByID)
}

输出:queryAllOffers

{
  offer {
    id
    name
    description
    logoURL
    opt_ins {
      id
      name
      description
    }
  }
}

输出:queryOfferById

{
  offer(id: "0001") {
    id
    name
    description
    logoURL
    opt_ins {
      id
      name
      description
    }
  }
}
英文:

GQL Queries

package main

import (
	"github.com/gookit/color"
)

const (
	offerInfo string = `{
	    id
	    name
	    description
	    logoURL
	    opt_ins {
	      id
	      name
	      description
	    }
	  }`
)

func QueryAllOffers() string {
	return `{ offer ` + offerInfo + `}`
}

func QueryOfferByID(id string) string {
	return `{
		offer (id: "` + string(id)  + `")` + offerInfo + ` }`
}

func main() {
	queryAllOffers := QueryAllOffers()
	color.Cyan.Println(queryAllOffers)

	offerID := "0001"
	queryOfferByID := QueryOfferByID(offerID)
	color.Blue.Println(queryOfferByID)
}

Output: queryAllOffers

{
  offer {
    id
    name
    description
    logoURL
    opt_ins {
      id
      name
      description
    }
  }
}

Output: queryOfferById

{
  offer(id: "0001") {
    id
    name
    description
    logoURL
    opt_ins {
      id
      name
      description
    }
  }
}

huangapple
  • 本文由 发表于 2014年1月22日 08:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/21272146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定