OneBite.Dev - Coding blog in a bite size

combine two strings in Go

Code snippet on how to combine two strings in Go

package main

import (
  "fmt"
)

func main() {
  var a string = "Hello"
  var b string = "World"
  fmt.Println(a +" "+ b)
}

This code example combines two strings, “Hello” and “World,” into one phrase. The first line is the package declaration, which names this code as a main package. The second line imports “fmt,” which is short for the formatting library, which contains the print functions. The third line then creates two new variables called “a” and “b” and assigns the strings “Hello” and “World” to them, respectively. Finally, the last line uses the fmt library’s “Println” function to combine the contents of the two variables and print the result to the terminal. This code example combines two strings together, “Hello” and “World,” into one phrase.

go