Go Strings

Updated:2018-07-08
Share:

Strings, which are widely used in Go programming, are a readonly slice of bytes. In the Go programming language, strings are slices. Go standard library provides many libraries to work with strings.

  • strings
  • regexp
  • unicode

Creating Strings in Go

To create a string you use douple quotes "" not a single quote, however you can use backtick ` for raw strings \n will be printed as \n not as a newline in raw strings, in the following snippet we use double qoutes:

welcome := "Welcome to coderme.com :)"

Here using backtick

welcome := `Welcome to coderme.com :)`

Go strings are UTF-8 sequences, internally named runes.

String Length in Go

You can use len() function to get the number the number of bytes in a string, if the string is purse ASCII or contains all single-byte characters then len() gets you the number of charachters too.

Example

package main

import "fmt"

func main() {

   greetMe :=  "Hello Me!"
   
   fmt.Println("greetMe's Length is:", len(greetMe))
}

Output

greetMe's Length is: 9

Concatenating Strings in Go

You can concatenate strings using strings package which includes many methods for srings manipulation.

strings.Join(elem []string, sep string)

strings.Join joins or concatenates the elements of an string slice []string using the second parameter to glue the slice, which place it between every teo elements of the slice.

example

package main

import (
       "fmt"
        "strings"
        )

func main() {

   website :=  []string{"coderme","com"} 
     
   fmt.Println(strings.Join(website, "."))
}

Output

coderme.com

Read also

Go Structures

Go Structures are a user-defined data type available in Golang, which allows you to combine different values types (including structures) into one type, its similar to Classes in other object-oriented programming languages, Assume you want to keep track of the articles in a blog. You might want to track the following attributes of each article − Title Body Category Author Defining a Structure To define a structure, you must use type and struct statements. The struct statement ...

Go Range

The range keyword is used to iterate over elements of an slice, array, map or channel. range over Arrays and Slices Use one variable to get the index of an element. Use two varaibles to get both index and the value of an element. range over Maps Use one variable to get the key of an element. Use two varaibles to get both key and the value of an element. range over Channels Use one variable to get the value of an element. How to use range ? ...

Python Decision Making

Decision-making is the way where a program shift execution from one side to another based on a condition if the condition evaluates to True then the next statement(s) will be executed other wise if there is any else  statement its block(or body) will be executed instead.   True vs False vs not True All non 'None' values, not empty string, non-zero numbers, whitspace-only strings, and not False values are True False All None, empty string, False values are conside...

C Functions

All C programs has at least one function, which is main() ,  and all the most simple programs define more functions. A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A functi...

C Recursion

Recursion in programming is the process of calling a procedure repeatedly in a self-similar way, a recursive function or routine is a function that call itself repeatedly. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an...