Golang program to create a string object


In Golang, a string cannot be modified after they have been created. A built-in type in Go, the string type can be used in a variety of ways much like any other data type. In this article, we will use three methods to create a string object. The first method involves using of double quotes and backticks, in the second method strings.Replace() function is used and in the third method, a byte function is used to execute the program.

Method 1: Using double quotes and backticks

Two string objects, str1 and str2, are created in this program. Backticks (') are used to generate the second string while double quotes (") are used to construct the first string. However, backticks enable for the creation of multi-line strings while both are similar in terms of syntax and functionality. Let’s see through the code and algorithm.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a function main and inside that function create a string str1 using double quotes.

  • Step 3 − Print str1 on the console using print statement in Golang.

  • Step 4 − Create another string str2 using backticks and print it on the console using print statement in Golang.

  • Step 5 − The print statement in Golang is executed using fmt.Println() function where ln means new line.

Example

In this example we will use double quotes and backticks to create a string object. Let’s see the working through code.

package main
import "fmt"

func main() {
   // Creating a string using double quotes
   str1 := "Hello, alexa!"  //create string using double quotes
   fmt.Println("The string created using double quotes is:")
   fmt.Println(str1)

   // Creating a string using backticks
   str2 := `Hello, alexa!`   //create string using backticks
   fmt.Println("The string created using backticks is:")
   fmt.Println(str2)
}

Output

The string created using double quotes is:
Hello, alexa!
The string created using backticks is:
Hello, alexa!

Method 2: Using strings.Replace function

The string "I am a frontend developer!" is produced in this example and given the value. To utilize the Replace function, which replaces the first instance of the string "frontend" with "backend" in the s string, the strings package must be loaded. After that, the outcome is printed to the console.

Syntax

strings.Replace()

In Go, the strings.Replace function is used to replace a specified number of occurrences of a string with another string.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that particular function create a string str "I am a frontend developer!".

  • Step 3 − Print the original string on the console using print statement in Golang.

  • Step 4 − In the next step, replace the frontend with backend in the same string using strings.Replace function and store it inside the str.

  • Step 5 − Print str on the console with the help of fmt.Println() function where ln means new line.

Example

package main
import (
   "fmt"
   "strings"
)

func main() {
   str := "I am a frontend developer!"  //create a string
   fmt.Println("The original string is:", str)

   str = strings.Replace(str, "frontend", "backend", 1)  //replace the value in string and print it on the console
   fmt.Println("The replaced string is:", str)
}

Output

The original string is: I am a frontend developer!
The replaced string is: I am a backend developer!

Method 3: Using make function

In this example, a byte slice with the starting length of 0 and the capacity of 100 is created using the make function. The phrase "I am a frontend developer!" is then added to str using the append function. The type conversion string(str) are applied to the outcome's byte slice to create a string, which is then written to the console.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a string using make function and append the string value inside the str using append function.

  • Step 3 − Print the string on the console using fmt.Println() function where ln means new line.

Example

In this example, we will use make and append function to create string object. Let’s see the execution through the code.

package main
import (
   "fmt"
)

func main() {
   str := make([]byte, 0, 100)   //create a string using make function
   str = append(str, "I am a frontend developer!"...)  //append the value in string 
   fmt.Println("String object created using make:", string(str)) //print the string
}

Output

String object created using make: I am a frontend developer!

Conclusion

We executed the program of creating a string object using three examples. In the first example, we created the object using double quotes and backticks. In the second example, we used strings.Replace() function and in the third example, we used the make function to execute the program.

Updated on: 20-Feb-2023

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements