4

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?

2
  • 1
    Go does not aim to be a scripting language. Commented Jan 22, 2014 at 6:48
  • Check out the package text/template if you are looking to produce parameterized text. Commented Jan 22, 2014 at 14:14

4 Answers 4

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Also worth noting that you can specify index positions in your string and not format in the exact same order of your parameters. e.g. - fmt.Printf("dstdir=%[2]s srcdir=%[1]s", srcdir, dstdir)
1

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

Comments

0

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".

Comments

0

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
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.