- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to make string immutable
Strings are by default immutable in Go. A string cannot be changed once it has been created. A compile-time error will appear if the value of a string is attempted to be changed. Therefore, there is no need to add any more logic to make it immutable. Let’s see how the execution is done. Here we are going to learn different techniquest of making a string immutable using go programming.
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 in that function initialize a string with some value in the string.
Step 3 − Change the string value in its reference and print it on the screen using fmt.Println() function where ln means new line.
Step 4 − Here, we cannot change the original string whereas its reference can be modified.
Example 1
In this example we will see how to make strings immutable using string literals. The output will be the string value printed on the screen using fmt.Println() which is a print function in Golang.
package main import "fmt" func main() { // Create an immutable string var str_val = "Hello, World!" str_val = "Cannot change this string" // string reference is given here fmt.Println("The string presented here is:") fmt.Println(str_val) //print the string value }
Output
The string presented here is: Cannot change this string
Example 2
Golang program to make string immutable using bytes.Buffer package in the example
package main import ( "bytes" "fmt" ) func main() { var buffer bytes.Buffer buffer.WriteString("Hello, World!") //add string to the buffer val := buffer.String() //store the buffer string in val fmt.Println("The string presented here is:") fmt.Println(val) //print string on the console buffer.WriteString("Cannot change this string") }
Output
The string presented here is: Hello, World!
Example 3
In this example, we will see how we can modify strings reference but not the original strings. We will concatenate the strings and print it on screen using print statement in Golang.
package main import "fmt" func main() { str := "hello" // create string fmt.Println("The original string is:", str) str = str + " world" //concatenation fmt.Println("The reference string in which new string concatenated is:") fmt.Println(str) // prints "hello world" // str is still "hello" }
Output
The original string is: hello The reference string in which new string concatenated is: hello world
Example 4
In this example, we will see how we can make strings immutable using byte and then casting it back to string. The output string will be printed on the screen.
package main import "fmt" func main() { byte_val := []byte("hello") //create byte slice byte_val[0] = 'E' //modify 0th value str := string(byte_val) //cast it to string fmt.Println("The immutability of string is presented as:") fmt.Println(str) // print the string on console }
Output
The immutability of string is presented as: Eello
Conclusion
We executed the program of making string immutable using 4 examples in the set. In the first example we used string literal to create string and then modified. In the second example we used bytes.Buffer package to make string immutable. In the third example we concatenated new value and in the fourth example we used byte slice and casted it to string. Hence, program executed successfully.