Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

Converting a byte array into a string


In this section, we're going to learn how to convert a byte array into a string: 

Note

The most important thing you need to know about this tutorial is that in Go, string variables are just a slice of bytes. Therefore, it is really easy to convert a byte array into a string value and a string value into a byte array.

  1. Let's see how we can start this. Imagine that you have a helloWorldByte array; currently, it is a literal byte array, but you can derive it from any stream, such as a network or a file:
package main

import "fmt"

func main(){
  helloWorldByte := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100}
  fmt.Println(string(helloWorldByte))
}
  1. We also have the string construct, which makes it really easy to convert a byte array into a string representation of it. We are going to use fmt.Println for the string representation of this helloWorldByte and run the code.
  2. So, let's run the code and check the output:
  1. As you can see, we converted the...