my-go-examples
A place to keep my go code snippets and examples.
These go examples also contain information
I gathered from other sources.
Table of Contents,
Documentation and reference,
GitHub Webpage
built with
concourse ci
GO EXAMPLES
All sections in alphabetical order.
API
BASIC PROGRAMMING
Just cool programs that are very useful and can be used as building blocks for
other cool programs.
-
json
Encode a struct to json and decode back to a struct.
-
read-file
Reading a file a few different ways.
-
singly-linked-list
A singly linked list (i.e. using just a head pointer).
BASIC SYNTAX
-
DATA TYPES
-
closure
An example of closure (a function that references variables from outside
its body).
-
pointers1
A struct passed to function by value (copy) and by address (reference).
-
pointers2
An int passed to function by address (reference) and by int pointer (reference).
-
slices-underlying-array
A slice being appended to showing the underlying array
being added to the slice.
-
FUNCTIONS
-
callback
Passing a function (as an argument) to a function. Not really used that often.
-
recursion
A function calling itself to make a fibonacci series.
-
shapes-using-functions
Calculating the area and perimeter of
circles, rectangles and triangles using functions.
Also refer to method and interface implementations below.
-
INTERFACES
-
interface
A simple example of an interface as parameter and an interface as a return.
Also see the next example that uses this in a package.
-
interface-package
The same example as above but the interface and methods placed in a package.
-
interfaces-card-atm-bank
Using interfaces to show how a bank card is
used to interact with an atm and bank.
-
shapes-using-interfaces
Calculating the area and perimeter of
circles, rectangles and triangles using interfaces.
Also refer to function and method implementations.
-
METHODS
BLOCKCHAIN
-
bitcoin-ledger
Demonstrates the bitcoin ledger in a blockchain using the
unspent transaction output model.
-
create-bitcoin-address-from-ecdsa-publickey
Create a bitcoin address from your ecdsa public key
using the crypto/ecdsa
standard package.
-
jeffCoin
Links to another repo
I made a working cryptocurrency. This link will take you to that repo.
-
single-node-blockchain-with-REST
A simple single node sha256 blockchain with a REST JSON API
(to view (GET) the blockchain and add (POST) a block).
CGO
Using c with go.
CLOUD SERVICES
CRYPTOGRAPHY
-
ASYMMETRIC CRYPTOGRAPHY
Great for digital signatures (verify sender) and receiving encrypted data.
-
ecdsa-digital-signature
To verify who a message is from,
create and verify a digital signature using the
crypto/ecdsa
standard package.
-
rsa-asymmetric-cryptography
Encrypt a message using a public key.
Decrypt the message using a private key.
Key pair generated using the crypto/rsa
standard package
(RSA is a cryptosystem for public key encryption).
-
rsa-asymmetric-cryptography-with-digital-signature
Using the above example, lets verify who the message is from.
Create and verify a digital signature using the
crypto/rsa
standard package.
-
HASHING
Great for getting fingerprints.
-
bcrypt-password-hashing
Store your password hash, not your password.
Set your password hash and then check your password using bcrypt.
-
md5-hash-from-file
Get an md5 hash (fingerprint) from an input file using the standard
crypto/md5
package.
I also added a flag to read in your .ssh/id_rsa.pub
key to get your ssh fingerprint.
Your github uses this for verification.
-
sha256-hash-from-file
Get an sha256 hash (fingerprint) from an input file using the standard
crypto/sha256
package.
-
SYMMETRIC CRYPTOGRAPHY
Using the same key to encrypt and decrypt.
-
aes-256
AES-256 No Mode
can only encrypt/decrypt 16 bytes of data.
-
aes-256-cbc
AES-256 CBC (Cipher Block Chaining) mode
where a block of plaintext is XORed with the previous cipherText block before
being encrypted.
-
aes-256-cfb
AES-256 CFB (Cipher FeedBack) mode
is almost identical to CBC mode but performed in reverse.
-
aes-256-ctr
AES-256 CTR (Counter) mode
has similar characteristics to OFB, but also
allows a random access property during decryption.
-
aes-256-gcm
I like this one
AES-256 GCM (Galois/Counter Mode)
is a block cipher counter mode with authentication.
-
aes-256-ofb
AES-256 OFB (Output FeedBack) mode
makes a block cipher into a synchronous stream cipher.
It generates keystream blocks, which are XORed with the plaintext blocks
to get the cipherText.
DATABASE
DISTRIBUTED SYSTEMS
GOROUTINES
Go is written for concurrency.
The go runtime schedules goroutines on threads.
The OS schedules these threads on cpus/cores.
JEFFS GO TEMPLATES
My starting point for all my go projects.
MESSAGING
-
protobuf
Using protobuf to show data -> marshal -> snd -> rcv -> unmarshal -> data
.
-
protobuf-NATS-publish-subscribe
Using NATS (publish/subscribe) as a pipe to send protobuf messages.
This is a model for one-to-many communication.
-
protobuf-NATS-queue-groups
Using NATS (queue groups) as a pipe to send protobuf messages.
This is a model for one-to-one communication.
-
protobuf-NATS-queue-groups-request-reply
I like this one
Using NATS (queue groups) as a pipe to send protobuf messages with a
request and getting a reply back from the subscriber.
This is a model for one-to-one communication.
-
protobuf-NATS-request-reply
Using NATS (request/reply) as a pipe to send protobuf messages.
This is a model for a subscriber sending a msg with a request and
getting a reply back from a subscriber.
This is also a model for one-to-many communication.
-
protobuf-NATS-request-reply-goroutines
Same as above protobuf-NATS-request-reply
but using goroutines for subscribers.
This is also a model for one-to-one communication.
PACKAGES
Refer to my repo
my-go-packages
for packages I created. Refer to
golang.org/pkg
for the standard go packages
-
errors
(github.com/pkg/errors)
Error Handling using non-standard github.com/pkg/errors
package.
-
flag
(standard)
The flag
package makes it easy to implement command-line flag parsing.
-
fmt-scan
(standard)
The fmt
package makes it easy to scan user input using fmt.Scan() by reading os.Stdin.
-
logrus
(github.com/sirupsen/logrus)
Logging using non-standard logrus
package.
-
net/http
(standard)
See above
http-GET-POST
and below
simple-webserver
examples.
-
test-jeffshapes
(github.com/JeffDeCola/my-go-packages/jeffshapes)
Testing the jeffshapes package (below).
SINGLE BOARD COMPUTERS
TESTING
-
gomock
Using gomock
on an interface and using the go tool gotests
for unit testing.
-
gotests
Using the go tool gotests
to test a very simple function and method.
-
gotests-complex-function
Using the go tool gotests
to test a function with complex inputs and outputs.
WEBSERVER
-
simple-webserver
Using the standard net/http
package to build a simple webserver.
I added a REST JSON API to this example in the above example
simple-webserver-with-REST.
-
simple-webserver-with-reactJS
Adding ReactJS, an open source UI, to to create a
single-page App (SPA) (Allowing content to be dynamically displayed
without requiring you to refresh or navigate to a different page).