Categorygithub.com/codedbyhasan/golang-for-beginners
repository
0.0.0-20250109122418-9d02c53cb6fd
Repository: https://github.com/codedbyhasan/golang-for-beginners.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

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

NameDecription
hello-worldPrints 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:

NameDescription
interfacesTwo different structs that implement a common interface.

Functions

A function can take zero or more arguments.

Examples of functions can be found below:

NameDescription
anonymousFunction that does not contain a name.
defer-statementDemonstrates defer statement.
higher-orderCalculates basic properties of a circle with example of higher order functions.
recursiveCalculates n! using recursion.
variadicFunctions that take 0 or more inputs of the same type.
examplesContains example functions.

Pointers

A pointer holds the memory address of a value.

Examples of pointers can be found below:

NameDescription
address-and-deferenceAddress (&) and dereference operators (*).
declare-pointerDeclaring a pointer.
dereference-pointerDereference pointer.
init-pointerInitialising pointer using different methods.
pass-by-referencePass by reference for different types including string, map and slice.
pass-by-valuePass by value.

Structs

A struct is a collection of fields.

Examples of structs can be found below:

NameDescription
accessing-fieldsAccessing fields within a struct.
comparing-structsComparing structs.
declare-structDeclare a struct.
init-structInitialising a struct using different methods.
pass-struct-to-funcPassing 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:

NameDescription
methodsSimple method.
method-setMethod 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:

NameDescription
anonymous-go-routineAnonymous goroutine.
main-go-routineThe main goroutine.
simple-exampleSimple example demonstrating a go routine.
wait-groupsAn example of a WaitGroup.
print-letters-and-numbersUsing 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:

NameDescription
read-and-writeSend and receive messages on unbuffered channels.
bufferedSend and receive messages on buffered channels.
closing-a-channelExample of closing a buffered channel.
for-rangeUsing a for-range to receive data from a buffered channel.
go-routine-leakA goroutine leak cause by a goroutine that remains active indefinitely.
select-statementUsing a select statement to wait on multiple communication operations.
closure-in-a-loopA goroutine closure executed in a for loop
time-out-codeAdding a timeout in a select statement when waiting on receiver/sender.
rx-and-tx-data-1Sending and received data using channels.
rx-and-tx-data-2Another 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:

NameDescription
example-packageGenerates 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.