Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning Go Programming
Learning Go Programming

Learning Go Programming: An insightful guide to learning the Go programming language

eBook
$29.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning Go Programming

Chapter 2. Go Language Essentials

In the previous chapter, we established the elemental characteristics that make Go a great language with which to create modern system programs. In this chapter, we dig deeper into the language's syntax to explore its components and features.

We will cover the following topics:

  • The Go source file
  • Identifiers
  • Variables
  • Constants
  • Operators

The Go source file

We have seen, in Chapter 1, A First Step in Go, some examples of Go programs. In this section, we will examine the Go source file. Let us consider the following source code file (which prints "Hello World" greetings in different languages):

The Go source file

golang.fyi/ch02/helloworld2.go

A typical Go source file, such as the one listed earlier, can be divided into three main sections, illustrated as follows:

  • The Package Clause:
          //1 Package Clause 
          package main 
    
  • The Import Declaration:
          //2 Import Declaration 
          import "fmt" 
          import "math/rand" 
          import "time" 
    
  • The Source Body:
          //3 Source Body 
          var greetings = [][]string{ 
            {"Hello, World!","English"}, 
            ... 
          } 
     
          func greeting() [] string { 
            ... 
          } 
     
          func main() { 
            ... 
          } 
    

The package clause indicates the name of the package this source file belongs to (see Chapter 6, Go Packages...

Go identifiers

Go identifiers are used to name program elements including packages, variables, functions, and types. The following summarizes some attributes about identifiers in Go:

  • Identifiers support the Unicode character set
  • The first position of an identifier must be a letter or an underscore
  • Idiomatic Go favors mixed caps (camel case) naming
  • Package-level identifiers must be unique across a given package
  • Identifiers must be unique within a code block (functions, control statements)

The blank identifier

The Go compiler is particularly strict about the use of declared identifiers for variables or packages. The basic rule is: you declare it, you must use it. If you attempt to compile code with unused identifiers such as variables or named packages, the compilers will not be pleased and will fail compilation.

Go allows you to turn off this behavior using the blank identifier, represented by the _ (underscore) character. Any declaration or assignment that uses the blank identifier is not bound...

Go variables

Go is a strictly typed language, which implies that all variables are named elements that are bound to both a value and a type. As you will see, the simplicity and flexibility of its syntax make declaring and initializing variables in Go feel more like a dynamically-typed language.

Variable declaration

Before you can use a variable in Go, it must be declared with a named identifier for future reference in the code. The long form of a variable declaration in Go follows the format shown here:

var <identifier list> <type>

The var keyword is used to declare one or more variable identifiers followed by the type of the variables. The following source code snippet shows an abbreviated program with several variables declared outside of the function main():

package main 
 
import "fmt" 
 
var name, desc string 
var radius int32 
var mass float64 
var active bool 
var satellites []string 
 
func main() { 
  name = "Sun" 
  desc = "Star" 
  radius...

Go constants

In Go, a constant is a value with a literal representation such as a string of text, Boolean, or numbers. The value for a constant is static and cannot be changed after initial assignment. While the concept they represent is simple, constants, however, have some interesting properties that make them useful, especially when working with numeric values.

Constant literals

Constants are values that can be represented by a text literal in the language. One of the most interesting properties of constants is that their literal representations can either be treated as typed or untyped values. Unlike variables, which are intrinsically bound to a type, constants can be stored as untyped values in memory space. Without that type constraint, numeric constant values, for instance, can be stored with great precision.

The followings are examples of valid constant literal values that can be expressed in Go:

"Mastering Go" 
'G' 
false 
111009 
2.71828 
94314483457513374347558557572455574926671352...

The Go source file


We have seen, in Chapter 1, A First Step in Go, some examples of Go programs. In this section, we will examine the Go source file. Let us consider the following source code file (which prints "Hello World" greetings in different languages):

golang.fyi/ch02/helloworld2.go

A typical Go source file, such as the one listed earlier, can be divided into three main sections, illustrated as follows:

  • The Package Clause:

          //1 Package Clause 
          package main 
    
  • The Import Declaration:

          //2 Import Declaration 
          import "fmt" 
          import "math/rand" 
          import "time" 
    
  • The Source Body:

          //3 Source Body 
          var greetings = [][]string{ 
            {"Hello, World!","English"}, 
            ... 
          } 
     
          func greeting() [] string { 
            ... 
          } 
     
          func main() { 
            ... 
          } 
    

The package clause indicates the name of the package this source file belongs...

Go identifiers


Go identifiers are used to name program elements including packages, variables, functions, and types. The following summarizes some attributes about identifiers in Go:

  • Identifiers support the Unicode character set

  • The first position of an identifier must be a letter or an underscore

  • Idiomatic Go favors mixed caps (camel case) naming

  • Package-level identifiers must be unique across a given package

  • Identifiers must be unique within a code block (functions, control statements)

The blank identifier

The Go compiler is particularly strict about the use of declared identifiers for variables or packages. The basic rule is: you declare it, you must use it. If you attempt to compile code with unused identifiers such as variables or named packages, the compilers will not be pleased and will fail compilation.

Go allows you to turn off this behavior using the blank identifier, represented by the _ (underscore) character. Any declaration or assignment that uses the blank identifier is not bound...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Insightful coverage of Go programming syntax, constructs, and idioms to help you understand Go code effectively
  • Push your Go skills, with topics such as, data types, channels, concurrency, object-oriented Go, testing, and network programming
  • Each chapter provides working code samples that are designed to help reader quickly understand respective topic

Description

The Go programming language has firmly established itself as a favorite for building complex and scalable system applications. Go offers a direct and practical approach to programming that let programmers write correct and predictable code using concurrency idioms and a full-featured standard library. This is a step-by-step, practical guide full of real world examples to help you get started with Go in no time at all. We start off by understanding the fundamentals of Go, followed by a detailed description of the Go data types, program structures and Maps. After this, you learn how to use Go concurrency idioms to avoid pitfalls and create programs that are exact in expected behavior. Next, you will be familiarized with the tools and libraries that are available in Go for writing and exercising tests, benchmarking, and code coverage. Finally, you will be able to utilize some of the most important features of GO such as, Network Programming and OS integration to build efficient applications. All the concepts are explained in a crisp and concise manner and by the end of this book; you would be able to create highly efficient programs that you can deploy over cloud.

Who is this book for?

If you have prior exposure to programming and are interested in learning the Go programming language, this book is designed for you. It will quickly run you through the basics of programming to let you exploit a number of features offered by Go programming language.

What you will learn

  • Install and configure the Go development environment to quickly get started with your first program.
  • Use the basic elements of the language including source code structure, variables, constants, and control flow primitives to quickly get started with Go
  • Gain practical insight into the use of Go s type system including basic and composite types such as maps, slices, and structs.
  • Use interface types and techniques such as embedding to create idiomatic object-oriented programs in Go.
  • Develop effective functions that are encapsulated in well-organized package structures with support for error handling and panic recovery.
  • Implement goroutine, channels, and other concurrency primitives to write highly-concurrent and safe Go code
  • Write tested and benchmarked code using Go's built test tools
  • Access OS resources by calling C libraries and interact with program environment at runtime

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 26, 2016
Length: 348 pages
Edition : 1st
Language : English
ISBN-13 : 9781784392338
Vendor :
Google
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 26, 2016
Length: 348 pages
Edition : 1st
Language : English
ISBN-13 : 9781784392338
Vendor :
Google
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 164.97
Learning Go Programming
$54.99
.Go Programming Blueprints
$54.99
Go Design Patterns
$54.99
Total $ 164.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. A First Step in Go Chevron down icon Chevron up icon
2. Go Language Essentials Chevron down icon Chevron up icon
3. Go Control Flow Chevron down icon Chevron up icon
4. Data Types Chevron down icon Chevron up icon
5. Functions in Go Chevron down icon Chevron up icon
6. Go Packages and Programs Chevron down icon Chevron up icon
7. Composite Types Chevron down icon Chevron up icon
8. Methods, Interfaces, and Objects Chevron down icon Chevron up icon
9. Concurrency Chevron down icon Chevron up icon
10. Data IO in Go Chevron down icon Chevron up icon
11. Writing Networked Services Chevron down icon Chevron up icon
12. Code Testing Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(5 Ratings)
5 star 80%
4 star 20%
3 star 0%
2 star 0%
1 star 0%
Shines Dec 03, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Vladimir Vivien's Learn Go Programming is a timely and practical hands-on guide to accelerate your learning curve with Google's fast growing program. Go is know for its ability to handle large, complex software in a team environment. Vladimir provides sample code, tips and warnings to help programmers at all levels avoid pitfalls. As a former IBM principal & E&Y consultant working with multinational corporations, I appreciate the value of what Vivien's Learn Go Programming can bring to individuals and large enterprise teams. Finally, as Director of Analytics and Continuous Improvement I see how Go plays a vital role for writing the code to manage complex server networks, needed to handle #big data, #machine learning and #IoT. I give Mr Vivien's Learn Go my highest recommendation
Amazon Verified review Amazon
Yemi Yisa Sep 25, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Buy the book. I LOVE his writing style. Straight to the point, not confusing, and extremely easy to digest.
Amazon Verified review Amazon
IOA M DOUNIS Nov 03, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I own dozens of programming language books and i have been programming for 24 years. This is one of the best introductory books i own on learning a new programming language, and the best, hands down, on the GO programming Language.If you want to learn programming in this powerful and wonderful language and you are already familiar with programming you must read this book first, the author is honest with his words, he wishes he had such a book when he begun learning GO, i wish the same!
Amazon Verified review Amazon
Manish kumar Jul 01, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
really nice for begginers and in details
Amazon Verified review Amazon
Jason S Chvat Apr 12, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good book overall. Straight forward, easy examples. My one issue is that it is literally riddled with typos and small code errors. I could tell based on previous programming experience where they were but if I didn't have the experience it could be very confusing
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.