• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Tutorials
      • Node.js Tutorial
      • Express.js Tutorial
      • Python Tutorial
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements

Strings in Go and its basic operations

  • August 11, 2023
  • CODE OF GEEKS
  • 0
In this Tutorial -:
  • Introduction
  • Prerequisites
  • Creating and Initializing Strings
  • Concatenating Strings
  • String Length
  • Slicing Strings
    • Slicing for Reversal
Strings in Go and its basic operations

Introduction

Strings are the fundamental building blocks of text-based data in programming. They play a crucial role in various operations, from displaying information to manipulating data.

In Go programming, strings are treated as a primary data type, and understanding their basic operations is essential for any developer.

In this tutorial, we will explore the core concepts of strings in Go and learn how to perform basic operations on them.



Prerequisites

Before we delve into the goto statement in Go, make sure you have Go installed on your system. If you haven’t already done so, you can download the latest version of Go from the official website: https://golang.org/dl/ Once installed, verify the installation by opening a terminal or command prompt and running the following command:

go version

If Go is installed correctly, it will display the version number.

Creating and Initializing Strings

In Go, a string is a sequence of characters enclosed in double quotes. You can create and initialize a string like this:

package main

import "fmt"

func main() {
    // Creating and initializing strings
    greeting := "Hello, Go!"
    fmt.Println(greeting)
}

O/P

Hello, Go!

Concatenating Strings

You can concatenate strings using the + operator:

package main

import "fmt"

func main() {
    firstName := "John"
    lastName := "Doe"

    fullName := firstName + " " + lastName
    fmt.Println("Full Name:", fullName)
}

O/P

Full Name: John Doe



String Length

To get the length of a string, you can use the built-in len() function:

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    text := "Go Programming"
    length := len(text)
    fmt.Println("Length:", length)

    // Length of string with non-ASCII characters
    nonASCII := "こんにちは"
    runeCount := utf8.RuneCountInString(nonASCII)
    fmt.Println("Rune Count:", runeCount)
}

O/P

Length: 14
Rune Count: 5

Slicing Strings

You can extract a portion of a string using slicing:

package main

import "fmt"

func main() {
    text := "Hello, World!"
    slice := text[0:5] // Slicing from index 0 to 4
    fmt.Println("Slice:", slice)
}

O/P

Slice: Hello

Slicing for Reversal

You can reverse a string using slicing:

package main

import "fmt"

func main() {
    text := "Hello, Go!"
    reversed := ""
    for i := len(text) - 1; i >= 0; i-- {
        reversed += string(text[i])
    }
    fmt.Println("Reversed:", reversed)
}

O/P

Reversed: !oG ,olleH

Strings and their basic operations are fundamental in Go programming. In this tutorial, we covered creating, concatenating, finding length, and slicing strings. Armed with this knowledge, you can manipulate strings effectively and build powerful applications in Go. Happy coding!

Remember, this is just the beginning. As you explore Go further, you’ll uncover more advanced string operations and techniques that can help you become a proficient Go developer.





Tags: Basic string operations in GoConcatenating strings in Go with examplesEfficient string handling in Go applicationsExploring string functions in GoGo language string handling guideGo language string slicing techniquesGo programming string data typeGo programming string manipulationGo string manipulation best practicesGo string manipulation tutorialHow to handle strings in GoMastering basic string operations in GoPractical examples of string operations in GoRetrieving string length in GoSlicing and dicing strings in GoSlicing strings in GoString concatenation in GoString manipulation tricks in GoUnderstanding string indexing in GoWorking with string length in Go programming
  • Next Introduction to Functions in Go programming

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Go Tutorials

  1. Introduction to Go
  2. Hello World with Go
  3. Tokens in Go
  4. Keywords in Go
  5. Comments in Go
  6. Datatypes in Go
  7. Variables & Constants in Go
  8. if-else in Go
  9. Switch Statement in Go
  10. Select Statement in Go
  11. for loop in Go
  12. break statement in Go
  13. continue statement in Go
  14. goto statement in Go
  15. Functions in Go
  16. Strings in Go
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions
Refund Policy
Contact us

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Tutorials

Python
Node.js
Express.js
Golang

Recent Posts

Strings in Go and its basic operations
  • August 11, 2023
Introduction to Functions in Go programming
  • August 11, 2023

Copyright @ CODE OF GEEKS. All Rights Reserved.