Skip to content

CodedByHasan/golang-for-beginners

Repository files navigation

Golang for Beginners

This repository contains contains simple examples of Golang concepts.

Repository Overview

Below is an outline of all examples contained in this repository.

Hello World

Name Decription
hello-world Prints Hello world to standard output.

Interfaces

An interface specifies a method set and is a useful way to introduce modularity. It is like a blueprint for a method set.

Examples of interfaces can be found below:

Name Description
interfaces Two different structs that implement a common interface.

Functions

A function can take zero or more arguments.

Examples of functions can be found below:

Name Description
anonymous Function that does not contain a name.
defer-statement Demonstrates defer statement.
higher-order Calculates basic properties of a circle with example of higher order functions.
recursive Calculates n! using recursion.
variadic Functions that take 0 or more inputs of the same type.
examples Contains example functions.

Pointers

A pointer holds the memory address of a value.

Examples of pointers can be found below:

Name Description
address-and-deference Address (&) and dereference operators (*).
declare-pointer Declaring a pointer.
dereference-pointer Dereference pointer.
init-pointer Initialising pointer using different methods.
pass-by-reference Pass by reference for different types including string, map and slice.
pass-by-value Pass by value.

Structs

A struct is a collection of fields.

Examples of structs can be found below:

Name Description
accessing-fields Accessing fields within a struct.
comparing-structs Comparing structs.
declare-struct Declare a struct.
init-struct Initialising a struct using different methods.
pass-struct-to-func Passing struct to a function by reference and value.

Methods

A method is a function with a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name.

Examples of methods can be found below:

Name Description
methods Simple method.
method-set Method set used to interface with a struct.

Goroutines

A goroutine is a lightweight thread managed by the Go runtime.

Examples of goroutines can be found below:

Name Description
anonymous-go-routine Anonymous goroutine.
main-go-routine The main goroutine.
simple-example Simple example demonstrating a go routine.
wait-groups An example of a WaitGroup.
print-letters-and-numbers Using goroutines to prints letter and numbers to the screen.

Channels

Channels are a typed conduit through which you can send and receive values with the channel operater <-.

Examples of channels can be found below:

Name Description
read-and-write Send and receive messages on unbuffered channels.
buffered Send and receive messages on buffered channels.
closing-a-channel Example of closing a buffered channel.
for-range Using a for-range to receive data from a buffered channel.
go-routine-leak A goroutine leak cause by a goroutine that remains active indefinitely.
select-statement Using a select statement to wait on multiple communication operations.
closure-in-a-loop A goroutine closure executed in a for loop
time-out-code Adding a timeout in a select statement when waiting on receiver/sender.
rx-and-tx-data-1 Sending and received data using channels.
rx-and-tx-data-2 Another example of sending and receiving data using channels.

Packages

Every go program is made up of packages. Programs start running in package main

An example package can be found below:

Name Description
example-package Generates a unique ID and encrypts a string and outputs to screen.

Running Locally

Assuming you are in . directory.

To run the different examples locally:

go run /path/to/main.go

For example, to run hello-world:

go run hello-world/main.go

Acknowledgements

There examples are obtained from KodeKloud's Golang for Beginners and Advanced Golang courses.

About

A project that demonstrates basic Golang concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages