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

Trimming spaces from the beginning and end of a string


Let's start with trimming spaces from the beginning and end of a string. There are many reasons why you may want to remove spaces from the beginning and end of a string; for instance, if you were accepting some values such as first name, you usually don't require any spaces at the end or at the beginning of that string value.

So, let's go ahead with our project and see how we can carry out this process in Go language. So, you have to add a new project for trimming spaces and have the main.go file that we're going to put our code in and then we're just going to run it; your screen should look something like this:

To begin, let's imagine that we have a string variable that has some spaces in it: 

package main
import (
  "fmt"
  "strings"
)
func main(){
  greetings := "\t Hello, World "
  fmt.Printf("%d %s\n", len(greetings), greetings)
}

In the preceding code snippet, /t is for tab and we have some space after it. There is hello World and...