Go Programming For Beginners – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Go Programming For Beginners – InApps in today’s post !

Read more about Go Programming For Beginners – InApps at Wikipedia



You can find content about Go Programming For Beginners – InApps from the Wikipedia website

For us, Go, also called golang, is one of the more interesting programming languages due to its use in applications that represent a new generation of loosely coupled apps and infrastructure. Docker, Cloud Foundry, CoreOS, Digital Ocean… all of these companies have made commitments to Go.

InApps, for us in many ways, represents a way to explore new thinking about technology. The diversity and rate of innovation means that it’s new for all of us at some point. With this in mind, we are introducing a new series of introductions to new stack technologies. For starters, we asked Kartik Nayak, an engineering student in Bangalore, to write his version of what a beginner’s guide to Go might look like. So, here you go, InApps Intros: Beginner’s Guide to Go. Cheers. Alex.

go muscat

Go, initially developed at Google, is a statically-typed language with syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.

Created By:

Go was initially developed in 2007 at Google and was released in November 2009. The current stable release is V1.3.

Usage of GO over Time:

go progress

Source : http://talks.golang.org/2014/hellogophers.slide

Why Go?

 Go is like a better C, from the guys that didn’t bring you C++” — Ikai Lan

 Features of Go:

  • Syntax and environment adopting patterns.
  • Fast compilation time.
  • Remote package management.
  • Online package documentation.
  • Built-in concurrency primitives.
  • Interfaces to replace virtual inheritance.
  • Type embedding to replace non-virtual inheritance.
  • Compiled into statically linked native binaries without external dependencies.
  • Simple language specifications.
  • Large built-in library.
  • Light testing framework.

The Go Language

Types

Integers

Integers are numbers without a decimal component. Unlike us, the computers use a base-2 representation called binary system. Go’s integer types are : uint8, int8, uint16, int16, uint32, int32, uint64, int64.

8, 16, 32 and 64 tell us how many bits each of the types use.

Also, a byte is an alias for uint8 and a rune for int32.

There are also 3 machine dependent integer types: uint, int and uintptr. They are machine dependent because their size depends on the type of architecture you are using.

Floating Point Numbers

Floating Point Numbers can be defined as numbers with a decimal component or more generally known as Real Numbers.

Go has two floating point types : float32 and float64

Go also has two complex number types : complex64 and complex128

Strings

A string is a sequence of characters of a definite length for textual representation.

String literals in Go can be created using either double quotes “Hello World” or back quotes Hello World.

Double quotes cannot contain newlines but allow special escape sequences like n and t.

Read More:   Serve Dynamic Content to Millions of Users at No Cost – InApps Technology 2022

Strings can be concatenated using the + symbol.

The length of a string can be obtained using the len(<string>) function.

Using indices, individual character of a string can be obtained.

Boolean

Booleans are a special 1 bit integer type used to indicate True or False.

The logical operators which can be used on booleans are :

Variables

Variables in Go are initialized using the var keyword and then followed by the variable name and variable type.

You can also initialize variables in a shorter way.

Here the data type is internally assigned.

You can also declare constants using the assignment operator, Numeric constants in Go are high-precision values.

Loops and Control Structures

For

For loops are used just as in C and Java.

or

The range keyword can be used to range over a list of values also

If

If statements are also similar to those in C and Java.

Switch

Switch statements starts with the keyword switch and follows with multiple cases.

Arrays, Slices and Maps

Arrays

The type [n]T is an array of size n of type T.

Slices

A slice is a segment of an array. The length of a slice is not fixed. Creation of slice is via the make command.

Maps

A map is an unordered collection of key-value pairs. Also known as an associative array, a hash table or a dictionary, maps are used to look up a value by its associated key.

Functions

Functions are defined using the keyword func followed by the function name and arguments and the return values.

If the result parameters are named, a return statement without arguments return the current values of the variables

Multiple return types

Go supports multiple return types.

Variadic Function

Functions can take 0 to undefined variables of a particular data type.

Closure

Consider the following example

Here we are actually assigning the function to a variable.

Similarly.

Here, the func increm returns a func of type func() int which still refers to the variable x which is outside the body of the func. This is known as a closure func.

Pointers

Pointers, yes pointers! Go has pointers, but no pointer arithmetic.

y points to x and the value of y is obtained using a * operator. The & operator is used to get the address of a variable.

The new operator is used to allocate memory to a pointer variable.

Structures, Methods and Interfaces

Structures

A structure is a user defined data type which contains named fields. A structure is basically an object in Go which has data types and methods on it.

We declare structures using the type and struct keyword. Individual elements of the structure are accessed using the dot operator.

Methods

Assume, we want to know what the dog/cow says, we can define a method on the given structure to perform a specific task, in our case to speak.

With this you can now use:

Here, the function speak takes a dog and cow types respectively and responds with a string based on the type.

Interfaces

Interfaces are basically a collection of methods. Lets define an interface on the function speak.

So, any type which has a method called speak automatically satisfies the Speaker interface.

Hence we can have.

In Go interfaces are implicitly satisfied, this one of the best features of the language.

Let us take an example, the Println function takes an interface which holds a method of type String() string

Read More:   Docker Defends Desktop Pricing, Says Support Led to Faster Features – InApps 2022

Defining our own method for the dog type we can print the data as per what is most suitable to us.

Because the dog has a String function and the interface taken by Println requires a String function, we satisfy that interface.

Concurrency

Go has a rich support for concurrency via goroutines and channels. This is one of the highlights of the language.

Goroutines

Goroutines can be described as functions which run concurrently from other functions. They’re invoked following the go keyword. As the function is invoked it runs concurrently and the rest of the code is executed without waiting for the completion of that function. The main function is implicitly a goroutine.

Now the function prints out all the no’s from different goroutines, as they’re concurrently run. The sleeper makes sure that a particular routine waits for 50ms before the next iteration, this gives time for the other routines to run.

Channels

For intercommunication of concurrent functions we use channels. Channels must be created before being used, using the chan keyword. Let us create a channel of type string.

Lets consider a small example

Here the sender  function continuously sends the string to the c channel and the receiver function prints the value received from the channel continuously.

The function prototype can also restrict the direction of data flow from and to the channel.

Let us replace the sender and the receiver function

There is also a select option when handling channels, this acts like a switch statement selecting the channel which is ready.

Final words

To move ahead with Go, have a look at the official documentation and I recommend the tour.

Also go through the talks held on Go, in various Gophercons I will be attending Gopeherconf India next year.

Have a look at the way Go code is organised and also how the testing framework is used in Go. Learn to use the automatic code formatting tool gofmt and also the automatic documentation generator godoc.

References

Kartik Nayak is an engineering student based in Bangalore, India. When not attending college, he can be seen learning about the linux kernel and coding. Also indulges in casual photography.

InApps is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker, Real.

Feature image by Chris Nokleberg via the Go website.



Source: InApps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...