GO Program to print last character in a string

How to print the last character in a string

Here we are explaining how to print the last character in a string in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

 

How to print the last character in a string in the GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to print the last character in a string in the Go language. Here variable str is holding the string to find the last character. And variable last for holding the last character of the string. Finally, the string's last character is found as str[len(str)-1] and print the result. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the string variable str

STEP 4: Assign str with the text "Go programming"

STEP 5: Find the last character of the string as last :=str[len(str)-1]

STEP 6: Finally print the variable last using fmt.Println()

 

GO Source Code

                                          package main
import "fmt"

func main() {

    var str string
    str= "Go programming"
    fmt.Println(str)
    last := str[len(str)-1]
    fmt.Printf("The last character in this string = %c\n", last)
}
                                      

OUTPUT

The last character in this string = g