GoLang - zhuje/openshift-wiki GitHub Wiki

Golang

Golang is primarily a backend language for

  • creating a backend in web development (webserver)
  • build components in Docker and Kubernetes

Variable Declaration

In Go, := is for declaration + assignment, whereas = is for assignment only.
For example, var foo int = 10 is the same as foo := 10.

Source:https://stackoverflow.com/questions/17891226/difference-between-and-operators-in-go

Pass by value vs Pass by reference

By default (if we're not using *dereferencing and &pointers)

  • Primitive types are pass-by-value
  • Arrays, Slice, Maps are pass-by-reference
  • Structs are pass-by-value

**Source**: https://david-yappeter.medium.com/golang-pass-by-value-vs-pass-by-reference-e48aac8b2716

Operators

'&' operator -- creates a pointer, gives the memory address
'*' operator -- dereferences the pointer, gives the value of what's found at the memory address

Primitives

// 'a *int' declare the parameter as a pointer that needs to be dereferenced
func Add(a *int){
     // dereference to get the value at this memory location, then increment 
     *a++ 
}

func main(){
    a := 0 
    
    // send the memory location of 'a' 
    Add(&a) 
     
    // now a = 1 
}

Arrays, Slices, Maps

func MapFunc(val map[string]interface{}) {
	val["this is a new value"] = 100
}

func main() {
	// Slices
	var arrInt []int = []int{1, 2, 3, 4, 5}
	var sliceInt = arrInt[3:]
	sliceInt[0] = 10
        // arrInt = {1, 2, 3, 10, 5}
        // sliceInt = {10,5}

	// MAP
	var emptyMap = make(map[string]interface{})
	MapFunc(emptyMap)
        // emptyMap = map[this is a new value:100]
}

Struct

type StructVal struct {
   IntVal: int 
} 

func (s StructVal) AddPtr() {
	s.IntVal++
}


func (s *StructVal) AddPtr() {
	s.IntVal++
}

func main(){
    init := StructVal{
       IntVal: 0 
    } 

    init.Add() // init = 0 
    init.AddPtr() // init = 1 
}

Implement TLS in GoLang

https://medium.com/@harsha.senarath/how-to-implement-tls-ft-golang-40b380aae288

CA = Certificate Authority TLS = Transport Layer Security

The following is how to create a self-signed certificate to test TLS (transport layer security) encryption in a GoLang application.

  1. Download openssl
  2. Run the command below which will generate ca.crt and ca.key
openssl req -new -newkey rsa:2048 -keyout ca.key -x509 -sha256 -days 365 -out ca.crt
  1. Create the server configuration file called server.cnf
[req]
default_md = sha256
prompt = no
req_extensions = v3_ext
distinguished_name = req_distinguished_name

[req_distinguished_name]
CN = localhost

[v3_ext]
keyUsage = critical,digitalSignature,keyEncipherment
extendedKeyUsage = critical,serverAuth,clientAuth
subjectAltName = DNS:localhost
  1. Generate the private key for the server
openssl genrsa -out server.key 2048
  1. Create a Certificate Signing Request
openssl req -new -key server.key -out server.csr -config server.cnf
  1. Create the certificate for the server signed with our self-signed CA. This generate a file called server.crt
penssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out server.crt -days 365 -sha256 -extfile server.cnf -extensions v3_ext

You'll be asked to re-enter your password from Step 2 when you generate a new key. Once you enter it, you'll generate another file called ca.srl which keeps track of the next available serial number for the CA

⚠️ **GitHub.com Fallback** ⚠️