https://yourbasic.org/golang/implement-stack/
A basic stack (LIFO) data structure · YourBasic Go
May 22, 2019 - CODE EXAMPLE You can implement a stack (LIFO queue) data structure in Go with a slice and the append function.
data structurebasicstacklifogo
https://yourbasic.org/
Algorithms to Go · YourBasic
Nov 11, 2022 - Texts about programming, algorithms and Go, with plenty of code examples.
algorithmsyourbasic
https://yourbasic.org/golang/round-float-2-decimal-places/
Round float to 2 decimal places · YourBasic Go
CODE EXAMPLE How to round a float to a string/float with 2 decimal places in Go.
decimal placesroundfloatyourbasicgo
https://yourbasic.org/golang/untyped-constants/
Untyped numeric constants with no limits · YourBasic Go
Jun 1, 2019 - Numeric constants in Go have no limits and can be untyped. When it's used in a context that requires a type, a type will be inferred and a limit applied.
numericconstantslimitsyourbasicgo
https://yourbasic.org/golang/for-loop-range-array-slice-map-channel/
4 basic range loop (for-each) patterns · YourBasic Go
Feb 17, 2020 - CODE EXAMPLE A range loop is used to iterate over the values in a slice, array or channel, the keys and values in a map, or the characters in a string.
basicrangelooppatternsgo
https://yourbasic.org/golang/data-races-explained/
Data races explained · YourBasic Go
Jul 20, 2019 - CODE EXAMPLE A data race is easily introduced by mistake and can lead to situations that are very hard to debug. This article explains how to avoid this...
dataracesexplainedyourbasicgo
https://yourbasic.org/golang/make-slice-map-channel/
Make slices, maps and channels · YourBasic Go
How to use the make function to create slices, maps and channels in Go.
makeslicesmapschannelsyourbasic
https://yourbasic.org/golang/trim-whitespace-from-string/
3 ways to trim whitespace (or other characters) from a string · YourBasic Go
Sep 15, 2018 - CODE EXAMPLE Use the strings.TrimSpace function to strip leading and trailing whitespace from a string.
waystrimwhitespacecharactersstring
https://yourbasic.org/golang/generate-random-string/
Generate a random string (password) · YourBasic Go
Mar 27, 2019 - CODE EXAMPLE How to generate a random string with restrictions (at least one special character, etc).
random stringgeneratepasswordyourbasicgo
https://yourbasic.org/golang/nutshells/
Go step by step · YourBasic Go
Apr 2, 2019 - Detailed descriptions of core Go concepts: interfaces, slices, maps, for loops, switch statements, packages.
gostepyourbasic
https://yourbasic.org/golang/channels-explained/
Channels offer synchronized communication · YourBasic Go
Jul 20, 2019 - CODE EXAMPLE A channel is a mechanism for two goroutines to synchronize execution and communicate by passing values.
channelsoffersynchronizedcommunicationyourbasic
https://yourbasic.org/golang/slice-of-keys-values-from-map/
Get slices of keys and values from a map · YourBasic Go
CODE EXAMPLE You can use a range statement to extract slices of keys and values from a map in Go.
getsliceskeysvaluesmap
https://yourbasic.org/golang/reverse-utf8-string/
How to reverse a string by byte or rune · YourBasic Go
Oct 18, 2018 - CODE EXAMPLE How to reverse a UTF-8 encoded string in Go.
reversestringbyteruneyourbasic
https://yourbasic.org/golang/do-while-loop/
2 patterns for a do-while loop in Go · YourBasic Go
Jan 18, 2019 - CODE EXAMPLE You can write a do-while or repeat-until loop in Go with a for statement.
patternsloopgoyourbasic
https://yourbasic.org/algorithms/
Algorithms explained · YourBasic
Nov 11, 2022 - Introduction to algorithms, data structures and algorithm analysis, all with plenty of code examples.
algorithmsexplainedyourbasic
https://yourbasic.org/golang/errors-explained/
Error handling best practice · YourBasic Go
Mar 12, 2019 - CODE EXAMPLE Go uses return values that implement the error interface to indicate errors. Panics are similar to C++ and Java exceptions, but are only intended...
error handlingbest practiceyourbasicgo
https://yourbasic.org/golang/detect-data-races/
How to detect data races · YourBasic Go
By starting your application with the '-race' option, the Go runtime might be able to detect and inform you about data races.
detectdataracesyourbasicgo
https://yourbasic.org/golang/int-vs-int64/
Pick the right one: int vs. int64 · YourBasic Go
Mar 19, 2019 - An index, length or capacity should normally be an int. The types int8, int16, int32, and int64 are best suited for data.
right onepickintvsyourbasic
https://yourbasic.org/golang/last-item-in-slice/
Last item in a slice/array · YourBasic Go
Sep 14, 2018 - CODE EXAMPLE Use a[len(a)-1] to access the last element of a.
lastitemslicearrayyourbasic
https://yourbasic.org/golang/generate-permutation-slice-string/
Generate all permutations · YourBasic Go
Jun 30, 2018 - CODE EXAMPLE How to generate all permutations of a slice or string in Go.
generatepermutationsyourbasicgo
https://yourbasic.org/golang/named-return-values-parameters/
Named return values [best practice] · YourBasic Go
Apr 2, 2019 - CODE EXAMPLE In Go result parameters may be named and used as regular variables.
best practicenamedreturnvaluesyourbasic
https://yourbasic.org/golang/string-functions-reference-cheat-sheet/
Go string handling overview [cheat sheet] · YourBasic Go
Feb 1, 2020 - CODE EXAMPLE 40+ essential string functions: literals, concatenation, equality, ordering, indexing, UTF-8, search, join, replace, split, trim, strip,...
cheat sheetgostringhandlingoverview
https://yourbasic.org/golang/regexp-cheat-sheet/
Regexp tutorial and cheat sheet · YourBasic Go
Apr 7, 2019 - Regular expressions in Go: introduction, cheat sheet and plenty of code examples.
cheat sheetregexptutorialyourbasicgo
https://yourbasic.org/golang/absolute-value-int-float/
Compute absolute value of an int/float · YourBasic Go
Jun 30, 2018 - CODE EXAMPLE Write your own code to compute the absolute value of an integer, but use math.Abs for floating point numbers.
absolute valuecomputeintfloatyourbasic
https://yourbasic.org/golang/find-type-of-object/
Find the type of an object · YourBasic Go
May 9, 2019 - You can find type information about an interface variable with the fmt package, type assertions, type switches or reflection.
findtypeobjectyourbasicgo
https://yourbasic.org/golang/go-java-tutorial/
Java to Go in-depth tutorial · YourBasic Go
Mar 11, 2020 - A tutorial to help Java programmers come up to speed quickly with Go. It compares the languages and offers plenty of code examples.
javagodepthtutorialyourbasic
https://yourbasic.org/golang/package-documentation/
Package documentation · YourBasic Go
Sep 15, 2018 - How to create and download Go package documentation.
packagedocumentationyourbasicgo
https://yourbasic.org/golang/convert-int-to-string/
Convert between int, int64 and string · YourBasic Go
Jun 26, 2019 - CODE EXAMPLE Use strconv.Itoa to convert an int to a decimal string, and strconv.Atoi to parse a string to an int.
convertintstringyourbasicgo
https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
Format byte size as kilobytes, megabytes, gigabytes, ... · YourBasic Go
Jul 1, 2018 - CODE EXAMPLE Utility functions for converting byte size to human-readable format. The code supports both SI and IEC formats.
formatbytesizeyourbasicgo
https://yourbasic.org/golang/find-search-contains-slice/
Find element in slice/array with linear or binary search · YourBasic Go
Jul 17, 2019 - CODE EXAMPLE To check if a slice or array contains an element, you can either write your own linear search or use one of the provided binary search functions.
binary searchfindelementslicearray
https://yourbasic.org/golang/redeclaring-variables/
Redeclaring variables · YourBasic Go
Variables can be redeclared in short multi-variable declarations where at least one new variable is introduced.
variablesyourbasicgo
https://yourbasic.org/golang/max-min-function/
Compute max of two ints/floats · YourBasic Go
Jun 30, 2018 - CODE EXAMPLE Write your own code to compute the minimum and maximum of integers, but use math.Min and math.Max for floating point numbers.
computemaxtwointsfloats
https://yourbasic.org/golang/check-prime/
Check if a number is prime · YourBasic Go
Apr 29, 2018 - CODE EXAMPLE To check if a number is prime in Go use the ProbablyPrime function from package math/big.
checknumberprimeyourbasicgo
https://yourbasic.org/golang/detect-deadlock/
How to debug deadlocks · YourBasic Go
Jul 20, 2019 - CODE EXAMPLE The Go runtime can often detect when a program freezes because of a deadlock. This article explains how to debug and solve such issues.
debugyourbasicgo
https://yourbasic.org/golang/how-to-sort-in-go/
The 3 ways to sort in Go · YourBasic Go
Mar 30, 2019 - CODE EXAMPLE How to sort any type of data in Go using the sort package. All algorithms in the package perform O(n log n) comparisons in the worst case.
wayssortgoyourbasic
https://yourbasic.org/golang/three-dots-ellipsis/
3 dots in 4 places · YourBasic Go
Mar 20, 2018 - The dot dot dot (...) notation is used in four different ways in Go: for variadic function parameters, as arguments to variadic functions, for array literals,...
dotsplacesyourbasicgo
https://yourbasic.org/golang/variadic-function/
Variadic functions (...T) · YourBasic Go
Aug 11, 2018 - CODE EXAMPLE A variadic function can be called with a variable number of arguments. You can pass a slice directly to a variadic function using the three dots...
functionsyourbasicgo
https://yourbasic.org/golang/remove-duplicate-whitespace/
Remove all duplicate whitespace · YourBasic Go
CODE EXAMPLE The regexp \s+ can be used to delete duplicate whitespace from a string.
removeduplicatewhitespaceyourbasicgo
https://yourbasic.org/golang/command-line-arguments/
Command-line arguments and flags · YourBasic Go
Sep 15, 2018 - CODE EXAMPLE You can access command-line arguments, including the program name and flags, through the os.Args variable.
command lineargumentsflagsyourbasicgo
https://yourbasic.org/golang/gotcha-function-doesnt-change-array/
Help: Array won’t change · YourBasic Go
Jun 1, 2019 - CODE EXAMPLE Arrays in Go are values: when you pass an array to a function it gets a copy of the original array data. If you want a function to update its...
helparraychangeyourbasicgo
https://yourbasic.org/golang/compiler-error-messages/
Learn to love your compiler · YourBasic Go
Feb 3, 2019 - Confusing Go compiler error messages: what they mean and how to fix the problem.
learnlovecompileryourbasicgo
https://yourbasic.org/golang/wait-for-goroutines-waitgroup/
Waiting for goroutines · YourBasic Go
Jul 20, 2019 - CODE EXAMPLE A sync.WaitGroup waits for a group of goroutines to finish.
waitingyourbasicgo
https://yourbasic.org/golang/bitwise-operator-cheat-sheet/
Bitwise operators [cheat sheet] · YourBasic Go
Aug 21, 2019 - To manipulate bits your can use operators (bitwise NOT, AND, OR, XOR, left and right shift) and functions in package math/bits (bitcount, rotate, reverse,...
cheat sheetbitwiseoperatorsyourbasicgo
https://yourbasic.org/golang/structs-explained/
Create, initialize and compare structs · YourBasic Go
Mar 8, 2019 - CODE EXAMPLE A struct is a typed collection of fields, useful for grouping data into records.
createinitializecomparestructsyourbasic
https://yourbasic.org/golang/overload-overwrite-optional-parameter/
Optional parameters, default parameter values and method overloading · YourBasic Go
Jan 29, 2019 - Go does not have optional parameters or default parameter values, nor does it support method overloading. Method overloading is emulated by writing several...
optionalparametersdefaultvaluesmethod
https://yourbasic.org/golang/gotcha-multiple-value-single-value-context/
Help: Multiple-value in single-value context · YourBasic Go
May 25, 2019 - CODE EXAMPLE When a function returns multiple values, you must take care of each one. You can use the blank identifier (underscore) to ignore unwanted return...
helpmultiplevaluesinglecontext
https://yourbasic.org/golang/shuffle-slice-array/
Shuffle a slice or array · YourBasic Go
Mar 27, 2019 - CODE EXAMPLE How to create a random permutation of a slice or array in Go.
shuffleslicearrayyourbasicgo
https://yourbasic.org/golang/compare-slices/
3 ways to compare slices (arrays) · YourBasic Go
Feb 6, 2019 - CODE EXAMPLE You typically want to write your own code to compare slices. However, use the optimized bytes.Equal for byte slices, and for testing consider...
wayscompareslicesarraysyourbasic
https://yourbasic.org/golang/recover-from-panic/
Panics, stack traces and how to recover [best practice] · YourBasic Go
Jul 17, 2019 - CODE EXAMPLE A panic stops the normal execution of a goroutine and prints a stack trace to the console. The recover function can be used to catch a panic and...
best practicepanicsstacktracesrecover
https://yourbasic.org/golang/operators/
Operators: complete list · YourBasic Go
Jun 13, 2019 - List of all Go operators (arithmetic, equality, comparison, logical, bitwise and other) with descriptions and operator priority.
complete listoperatorsyourbasicgo
https://yourbasic.org/golang/defer/
Defer a function call (with return value) · YourBasic Go
May 9, 2019 - CODE EXAMPLE A deferred function is executed when the surrounding function returns, either on exit or on panic. Deferred functions are commonly used for...
deferfunctioncallreturnvalue
https://yourbasic.org/golang/interface-to-string/
Convert interface to string · YourBasic Go
To get a string representation of an interface value in Go, use the fmt.Sprintf function.
convertinterfacestringyourbasicgo
https://yourbasic.org/about/
About · YourBasic
Jul 29, 2025 - This is where Stefan Nilsson writes about coding, algorithms, data structures and Go.
yourbasic
https://yourbasic.org/golang/days-between-dates/
Days between two dates · YourBasic Go
Oct 25, 2018 - CODE EXAMPLE How to calculate the number of days between two dates in Go.
daystwodatesyourbasicgo
https://yourbasic.org/golang/generate-uuid-guid/
Generate a unique string (UUID, GUID) · YourBasic Go
Apr 2, 2019 - CODE EXAMPLE You can use the rand.Read function from package crypto/rand to generate a basic UUID.
generateuniquestringuuidguid
https://yourbasic.org/golang/slices-explained/
Slices/arrays explained: create, index, slice, iterate · YourBasic Go
Feb 11, 2020 - CODE EXAMPLE A slice refers to a section of an underlying array. It can grow and shrink within the bounds of this array.
slicesarraysexplainedcreateindex
https://yourbasic.org/golang/package-init-function-main-execution-order/
Package initialization and program execution order · YourBasic Go
Sep 15, 2018 - Imported packages are initialized before the package itself. Packages are initialized one at a time: first package-level variables are initialized in...
packageinitializationprogramexecutionorder
https://yourbasic.org/golang/getting-started-hello-world/
Go beginner’s guide: top 4 resources to get you started · YourBasic Go
Apr 12, 2019 - How to write 'Hello world' and get started with Go programming: try it out, learn the basics, install the tools, and start a project.
guide topgoresourcesgetstarted
https://yourbasic.org/golang/day-month-year-from-time/
Get year, month, day from time · YourBasic Go
May 19, 2018 - CODE EXAMPLE How to get the day/month/year as integers from a time.Time in Go.
getyearmonthdaytime
https://yourbasic.org/golang/tutorials/
Go tutorials · YourBasic Go
Apr 22, 2018 - Golang tutorials for beginners and experienced developers alike: best practices and production-quality code examples.
gotutorialsyourbasic
https://yourbasic.org/golang/convert-string-to-float/
Convert between float and string · YourBasic Go
Jan 18, 2019 - CODE EXAMPLE Use strconv.ParseFloat to parse a floating point number from a string, and fmt.Sprintf to format a float as a string.
convertfloatstringyourbasicgo
https://yourbasic.org/golang/efficient-parallel-computation/
3 rules for efficient parallel computation · YourBasic Go
Jul 20, 2019 - CODE EXAMPLE To efficiently schedule parallel computation on separate CPUs is more of an art than a science. This article gives some rules of thumb.
rulesefficientparallelcomputationyourbasic
https://yourbasic.org/golang/function-pointer-type-declaration/
Function types and values · YourBasic Go
Jun 29, 2018 - CODE EXAMPLE Functions in Go are first class citizens: function types and function values can be used and passed around just like other values.
functiontypesvaluesyourbasicgo
https://yourbasic.org/golang/log-to-file/
Write log to file (or /dev/null) · YourBasic Go
Apr 12, 2019 - CODE EXAMPLE Use os.OpenFile with arguments os.O_APPEND | os.O_CREATE | os.O_WRONLY and 0644 to create and open a log file. To disable all output from a logger...
dev nullwritelogfileyourbasic
https://yourbasic.org/golang/
Go go-to guide · YourBasic
Nov 4, 2022 - Become a better Go programmer with these tutorials and code examples.
goguideyourbasic
https://yourbasic.org/golang/io-reader-interface-explained/
How to use the io.Reader interface · YourBasic Go
Sep 15, 2018 - CODE EXAMPLE An io.Reader is an entity from which you can read a stream of bytes. The standard library has many Reader implementations, including in-memory...
useioreaderinterfaceyourbasic
https://yourbasic.org/golang/current-time/
How to get current timestamp · YourBasic Go
Jun 27, 2018 - CODE EXAMPLE To get a second/nanosecond timestamp use time.Now and time.Unix or time.UnixNano.
get currenttimestampyourbasicgo
https://yourbasic.org/golang/concurrent-programming/
Concurrent programming · YourBasic Go
Feb 22, 2019 - This tutorial covers concurrency patterns, goroutines, channels, deadlock, data race, timer, ticker, waitgroup, and efficient parallel computation.
concurrentprogrammingyourbasicgo
https://yourbasic.org/golang/convert-string-to-rune-slice/
Convert between rune array/slice and string · YourBasic Go
Mar 12, 2019 - CODE EXAMPLE When you convert a string to a rune slice (array), you get a new slice that contains the Unicode code points (runes) of the string.
convertrunearrayslicestring
https://yourbasic.org/golang/round-float-to-int/
Round float to integer value · YourBasic Go
CODE EXAMPLE How to round a float64 to the nearest integer: round away from zero, round to even number, convert to an int type.
roundfloatintegervalueyourbasic
https://yourbasic.org/golang/copy-explained/
How to use the copy function · YourBasic Go
Sep 14, 2018 - The copy function copies elements to and from a slice. It returns the number of elements copied, which will be the minimum of len(dst) and len(src).
usecopyfunctionyourbasicgo
https://yourbasic.org/golang/hash-md5-sha256-string-file/
Hash checksums: MD5, SHA-1, SHA-256 · YourBasic Go
Jun 23, 2018 - CODE EXAMPLE To compute the hash value of a string or byte slice, use the Sum function from crypto package md5, sha1 or sha256. For a file or input stream you...
hashchecksumsshayourbasicgo
https://yourbasic.org/golang/constructor-best-practice/
Constructors deconstructed [best practice] · YourBasic Go
May 12, 2018 - CODE EXAMPLE Best practice in Go is to use suitable zero values and factories instead of constructors.
best practiceconstructorsdeconstructedyourbasicgo
https://yourbasic.org/golang/day-of-week-int/
How to find the day of week · YourBasic Go
May 19, 2018 - CODE EXAMPLE How to get the day of week as an integer from a time.Time in Go.
finddayweekyourbasicgo
https://yourbasic.org/golang/underscore/
Blank identifier (underscore) · YourBasic Go
Apr 24, 2018 - The blank identifier is an anonymous placeholder. It can be used to ignore values, import only for side effects, or to silence the compiler.
blankidentifierunderscoreyourbasicgo
https://yourbasic.org/golang/implement-set/
2 basic set implementations · YourBasic Go
Jan 25, 2019 - CODE EXAMPLE To implement a set in Go you can use a key-value map with boolean or empty struct values.
basic setimplementationsyourbasicgo
https://yourbasic.org/golang/switch-statement/
5 switch statement patterns · YourBasic Go
Jul 16, 2019 - CODE EXAMPLE A switch-case-default statement is a shorter and cleaner way to write a sequence of if-else statements.
switchstatementpatternsyourbasicgo
https://yourbasic.org/golang/pointer-vs-value-receiver/
Pointer vs. value receiver · YourBasic Go
Apr 14, 2018 - Guidelines on how to choose between pointer and value receivers for Go methods.
pointervsvaluereceiveryourbasic
https://yourbasic.org/golang/convert-string-to-byte-slice/
Convert between byte array/slice and string · YourBasic Go
Mar 12, 2019 - CODE EXAMPLE When you convert between a string and a byte slice (array), you get a brand new slice that contains the same bytes as the string, and vice versa.
byte arrayconvertslicestringyourbasic
https://yourbasic.org/golang/max-min-int-uint/
Maximum value of an int · YourBasic Go
CODE EXAMPLE The max and min values of an int can be computed as untyped constants.
maximumvalueintyourbasicgo
https://yourbasic.org/golang/append-explained/
How to append anything (element, slice or string) to a slice · YourBasic Go
May 14, 2019 - CODE EXAMPLE The append function appends elements to the end of a slice: if there is enough capacity, the underlying array is reused; if not, a new underlying...
appendanythingelementslicestring
https://yourbasic.org/golang/check-map-contains-key/
3 ways to find a key in a map · YourBasic Go
May 17, 2019 - CODE EXAMPLE To check if a map contains a key, use the second return value. It's a boolean that indicates if the key exists.
waysfindkeymapyourbasic
https://yourbasic.org/golang/append-to-file/
Append text to a file · YourBasic Go
CODE EXAMPLE To open/create a file for writing and appending in Go: os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
appendtextfileyourbasicgo
https://yourbasic.org/golang/rune/
Runes and character encoding · YourBasic Go
Mar 23, 2019 - A rune is a type meant to represent a Unicode code point. Strings, however, are sequences of bytes (typically containing Unicode text encoded in UTF-8).
runescharacterencodingyourbasicgo