Printing and formatting

In Go (Golang), you can print and format strings using the fmt package, which provides functions for formatted I/O. Here are some common ways to print and format strings in Go:

Printing plain text:

You can use fmt.Println to print plain text to the standard output (usually the terminal or console):

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Formatting strings with placeholders:

You can format strings using placeholders and the fmt.Printf function, which is similar to the printf function in C.

package main

import "fmt"

func main() {
    name := "John"
    age := 30
    fmt.Printf("Name: %s, Age: %d\n", name, age)
}

In the format string, %s is a placeholder for a string, and %d is a placeholder for an integer

Sprintf for formatting into a string

If you want to format a string and store it in a variable, you can use fmt.Sprintf

package main

import "fmt"

func main() {
    name := "Alice"
    age := 25
    formattedString := fmt.Sprintf("Name: %s, Age: %d", name, age)
    fmt.Println(formattedString)
}

4. String concatenation:

You can also concatenate strings using the + operator, but this doesn't provide the same level of control and formatting as the previous methods. It's a simple way to combine strings:

package main

import "fmt"

func main() {
    str1 := "Hello, "
    str2 := "World!"
    concatenatedString := str1 + str2
    fmt.Println(concatenatedString)
}