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
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781784395438
Vendor :
Google
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 125.97
Learning Go Programming
€41.99
.Go Programming Blueprints
€41.99
Go Design Patterns
€41.99
Total 125.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.