7.8 C
New York
Friday, November 25, 2022

Everything You Need to Know to Start Writing Go – MUO – MakeUseOf

Get up to speed with this primer on the Go language.
Go is one of the modern programming languages gaining traction on many surveys as developers preferred language. Go has an easy-to-use and understand syntax while providing top-tier performance.
You can use Go to build various applications, from web apps to command-line apps, cloud services, and networking. Go’s ecosystem of third-party packages supports many other use cases.
Go has most of the features you’d expect in a modern language: generics, concurrency, type inference, garbage collection, and many more.
You can run Go on most operating systems. Head to the installations page and download a preferred Go version for your operating system.
Once you’ve downloaded and installed Go, you can start writing and running Go code in files with a .go file extension.
You’ll find Go has most of the features and much of the functionality of other programming languages. If you have previous programming experience, you should find Go straightforward.
Go is quite expressive on the fundamental level. There are two ways you can declare variables in Go. You can use the var keyword to declare variables of various data types. After specifying the identifier, you’ll have to set the variable's data type.
You can use any as the data type if you’re uncertain of the variable's data type.
You can also declare constants with the const keyword in a similar fashion to declaring variables.
It is impossible to modify constants after their declaration.
Go provides an alternative way to declare variables in functions. Note the use of a colon before the equals sign:
Go also has conditionals for decision-making in programs. You can use the if and else statements in your code to handle decisions.
Here’s an example if statement that compares two integers for equality:
You can only use else statements after specifying an if statement, and you have to specify the else block after the closing if block:
The else block runs only when the if statement evaluates to false. Go doesn’t provide else-if statements, but you can use switch statements for complex conditional statements.
Here’s the anatomy of a switch statement in Go.
You can create switch statements with the switch keyword, after which you can specify different cases with the case keyword. You can handle the default case using a default keyword.
Go provides for-loops for repetitive tasks, and unlike most languages, there’s no while or do-while loop in Go.
You can use the popular C-style for-loop or the range for-loop that certain data structures support.
Here’s an example of using the C-style for loop in a Go program:
You can use Go’s built-in range for-loop on compound data structures like slices, maps, and arrays. The range function returns the index and element of the index as it traverses the data structure.
Arrays are one of the compound data types in Go. Go arrays are similar to C-style arrays and have a definite length on declaration and instantiation.
Here’s how you can declare arrays in Go:
You can use indexing to access, assign and update elements in a position of the array:
The code above updates or inserts the string as the fourth entry of the arr array variable.
Go provides slices as an alternative to arrays for dealing with data of indefinite length. Slices are similar to arrays, except that you can change the length of slices.
You’ll need to use the built-in make function to create a slice. Pass in the slice’s data type and initial length to the make function.
You can use the append function to insert elements into slices. By default, the append function inserts elements at the end of the slice.
Append operations on slices can be expensive to work with since Go creates a new array on each append operation.
Maps are the built-in associative (key-value pair) data type in Go. You can use the make function to create a map or a simple declaration where you’ll have to instantiate the map.
You can access values in a map by specifying the keys. Also, you can insert values into a map by specifying a key-value pair.
You can use the delete function to remove key-value pairs from maps. The delete function takes in the map’s identifier and the key of the pair you want to remove:
Functions are the tool for code reusability in Go. You can declare functions with the func keyword followed by the function’s identifier:
Functions can accept arguments and return values. You’ll have to specify the argument type alongside the identifier for arguments.
You’ll specify the return type before the function’s code block and return a value of the specified type for functions that return values.
Go isn’t an object-oriented language by design, but you can implement object-oriented features in Go using structs.
Structs are user-defined types for grouping other data types into a single entity. Go structs can hold values of any Go-supported types, and functions can implement structs.
Here’s how you can declare a struct in Go:
The Game struct has fields with types of map, string, integer, and floating point. You can instantiate structs with default values or assign values to them.
Functions can implement and access struct types. You’ll have to specify the struct parameter before the function’s identifier.
When you pass a struct to a function, the function has access to the struct’s fields, and the function becomes a struct method.
You’ve learned the basics of the Go programming language and the syntax to start writing Go programs.
There are many fields and applications where you can use Go. Go is popularly used as a server-side language, and you can always explore building web-based applications with it too.
Goodness is a mechanical engineering student and software developer passionate about cloud technologies and the Go programming language.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


Related Articles


Please enter your comment!
Please enter your name here

Latest Articles