Go Program to Print First Character in a String

Write a Go Program to find and Print the First Character in a String. We can use the index position to access the string characters. Here, strFirstChar[0] returns the first character in a given string strFirstChar.

package main

import "fmt"

func main() {

    var strFirstChar string

    strFirstChar = "Tutorial Gateway"

    fmt.Println(strFirstChar)

    firstChar := strFirstChar[0]

    fmt.Printf("The First Character in this String = %c\n", firstChar)
}
Go Program to Print First Character in a String 1

The above string first character code returns the byte as an output. However, you can Golang slice a string that gets and prints the first string character.

package main

import "fmt"

func main() {

    var strFirstChar string

    strFirstChar = "Go Programs"

    fmt.Println(strFirstChar)

    firstChar := strFirstChar[:1]

    fmt.Println("The First Character in this String = ", firstChar)
}
Go Programs
The First Character in this String =  G

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.