ADVERTISEMENT
ADVERTISEMENT

How to assign string to bytes array in Golang?

Go language | Learn, how to assign a string to the bytes array and how to append a string at the end of the bytes array?
Submitted by IncludeHelp, on October 19, 2021

The simple, easy, and safest way is to assign a string to bytes array is,

[]byte("String_Value")

Consider the below example,

// Go language program to assign string
// to bytes array

package main

import (
	"fmt"
)

// Main function
func main() {
	// Declare a byte array
	var b []byte

	// Assigning the string to bytes array
	b = []byte("Hello, world!")

	// Printing the type and value
	// of bytes array
	fmt.Printf("%T, %v\n", b, b)

	// Appending the string at the end of
	// the bytes array
	b = append(b, []byte("Okay!")...)
	fmt.Printf("%T, %v\n", b, b)
}

Output:

[]uint8, [72 101 108 108 111 44 32 119 111 114 108 100 33]
[]uint8, [72 101 108 108 111 44 32 119 111 114 108 100 33 79 107 97 121 33]

Golang FAQ »

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.