eonil
eonil

Reputation: 86095

Variable capturing in string literal in 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?

Upvotes: 4

Views: 12926

Answers (4)

Flavio
Flavio

Reputation: 546

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

Upvotes: 0

Flavio
Flavio

Reputation: 546

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

Upvotes: 1

kostix
kostix

Reputation: 55533

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

Upvotes: 0

Eve Freeman
Eve Freeman

Reputation: 33175

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)

Upvotes: 5

Related Questions