how to print string in golang
To print a string in Go, you can use the fmt
package. Here are the steps to print a string in Go:
- Import the
fmt
package: In order to use thefmt
package, you need to import it at the beginning of your Go file. You can do this by adding the following line of code:
import "fmt"
- Declare a string variable: Before printing the string, you need to declare a variable to hold the string value. You can declare a string variable like this:
var str string = "Hello, World!"
- Use
fmt.Println()
function: To print the string, you can use thefmt.Println()
function. This function takes the string as an argument and prints it to the console. Here's an example:
fmt.Println(str)
- Run the Go program: Save the file with a
.go
extension, and then you can run the program using thego run
command followed by the file name. For example:
go run main.go
When you run the program, it will print the string "Hello, World!" to the console.
That's it! These are the steps to print a string in Go using the fmt
package.