Overview
Below is a simple way to initialize or create a string in Go. In the below program, we have simply declared as well as defined a string named sample
Notice the syntax
sample := "test"
Here is the full program
Program
package main
import "fmt"
func main() {
sample := "test"
fmt.Println(sample)
}
Output
test
If we want to only declare a string variable then below is the way. A declared string is initialized to an empty string
package main
import "fmt"
func main() {
var sample string
fmt.Println(sample)
}
Output
Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial
Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –
Note: Check out our system design tutorial series System Design Questions