Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Last active August 23, 2017 22:05
Show Gist options
  • Save hectorgool/6a1fdce4e351ff2c36888fdf3d618711 to your computer and use it in GitHub Desktop.
Save hectorgool/6a1fdce4e351ff2c36888fdf3d618711 to your computer and use it in GitHub Desktop.
How to create multiline strings in Golang
/*
How to create multiline strings in Golang
twitter@hector_gool
https://play.golang.org/p/iR5Tt-iwSR
*/
package main
import (
"fmt"
"strconv"
)
func main() {
//the first solution
fmt.Println("curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/1?pretty -d '{")
fmt.Println(" \"firstname\" : \"Rodolfo\",")
fmt.Println(" \"lastname\" : \"Guzmán Huerta\",")
fmt.Println(" \"alias\" : \"El Santo\"")
fmt.Println("}'")
curl1 :=
`
curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/1?pretty -d '{
"firstname" : "Rodolfo",
"lastname" : "Guzmán Huerta",
"alias" : "El Santo"
}'
`
fmt.Println(curl1)
id := 2
firstname := "Daniel"
lastname := "García Arteaga"
alias := "Huracán Ramírez"
// the second solutión
fmt.Println("The second solutión:\n")
fmt.Printf("curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/%v?pretty -d '{", id)
fmt.Printf(" \"firstname\" : \"%v\",", firstname)
fmt.Printf(" \"lastname\" : \"%v\",", lastname)
fmt.Printf(" \"alias\" : \"%v\"", alias)
fmt.Printf("}'")
fmt.Println("\n\nThe best solutión:\n")
// the Itoa function of the package strconv is to convert an int value to string
// the best solution
curl2 :=
`
curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/` + strconv.Itoa(id) + `?pretty -d '{
"firstname" : "` + firstname + `",
"lastname" : "` + lastname + `",
"alias" : "` + alias + `"
}'
`
fmt.Println(curl2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment