Introduction

  1. An open source Programming language created at Google.
    1. Created as an internal programming language for Google infrastructure.
  2. Go is simple, minimal and progmatic
    1. Emphasis on solving practical aspect.
  3. Modern General purpose language
    1. Go is widely used for building backend systems
    2. Build System based applications as well as Infrastructue Tools like docker,Kubernetes,Terraform.
    3. Build for large scale backend and distributed systems
    4. Rust can be used for Systems level applications but it is complex but performs slightly better than Go.
  4. Statically typed language with high productivity.
    1. We specifically provide the data type.
    2. Easy to debug
  5. Compiles to Native machine code
    1. It complies quickly as compared to Other languages.
    2. Java compiles to intermediate byte code.
    3. We don’t need to package compiler separately.
  6. Garbage collected language
    1. Garbage collection is done automatically, which cannot be controlled manually.
  7. Inbuilt concurrency
    1. Can Leverage Multiple CPU cores
    2. Run autonomous jobs Independent of each other
  8. First appeared in Go 2009
  9. Gopher is the mascot of Go
  10. Go 1.0 was Released in March 2012
  11. Current version is 1.26
  12. Set up the development environment
    1. Download and, install Go: https://go.dev
    2. Create a GOPATH envirnoment variable
      1. 'export GOPATH=$HOME/go' >> ~/.bashrc
    3. Set the GOMODCACHE environment variable (Eg: export GOMODCACHE=$HOME/go/pkg).
      1. The directory where third-party libraries will be dowloaded
      2. export GOMODCACHE=$GOPATH/pkg
    4. Setup following environment variables
      1. GOMODCACHE=$GOPATH/pkg

      2. GOBIN=$GOPATH/bin

    5. Set the GOBIN environment variable - The directory where go commands will place binaries (executables)
      1. export GOBIN=$GOPATH/bin

      2. go install - Installs executables into GOBIN directory
    6. Add GOBIN to system path
      1. echo 'export PATH=$PATH:$GOBIN' >> ~/.bashrc
    7. Install GIT: https://git-scm.com
    8. Install an IDE, VS Code: https://code.visualstudio.com or use GoLand(https://www.jetbrains.com/go/)
      1. GOLand is a dedicated ID
    9. Check Installation with following commands
      1. go env GOMODCACHE
      2. go env GOBIN
      3. go env
      4. go version
  13. Starting up project
    1. create a root directory for your project
    2. Use command go mod init "{module_name}" to create a go.mod file
      1. This is a dependency management file
      2. This includes any third party libraries which we intend to use in our project.
      3. It is a mandatory file even if we don't use any dependencies.
      4.  Use command go mod init "{module_name}" to create  file
  14. In Linux Go Installation path is
    1. /usr/lib/golang
  15. Go has an opensource eco system.
    1. No License required for any libraries.
  16. Disadvantages
    1. It is not a very much feature rich programming language.
    2. C an C++ can perform bertter than go under certain conditions but Go performs better than Java and C#.
  17. In Go, we give two kinds of package names, either main or any other name.
    1. Main means It compiles to executables.
    2. Any name, other than main Then it is a library.
  18. Add import block and add libraries
    1. Standard libraries which are installed with Go.
    2. Third-party libraries Which are imported from vendors.
    3. Import defines the import block to import libraries
  19. Main is the entry point to executable programs
  20. Library packages Do not include main
  21. Go build command compiles all the “.go” source files from the directory and put the resulting binary into the same directory.
  22. To change the name of executable Use -o command 
    1. Go build -o “{Executable – name}”
  23. To create another Operating System specific build file use GOOS=Windows GOARCH=amd64 go build.
  24. Multiple main functions are not allowed.
    1. There should be only one entry point
  25. The executables created from Go do not need to install Go in another environment to run. These already contain some essential Go environment aspects.
  26. Go executables are very lightweight
    1. Very low and lightweight operating systems(Linux distribution) Have been created with Go. These require very less memory.
  27. Go install creates a build And install executables In GOBIN location.
  28. Go run Is used to run a Go file during development phase. It does not create a build file.
    1. We should always specify a file name with Go run command.
    2. It create an executable at temporary location.
    3. We can specify multiple files in Go run command.
  29. We can pass multiple files to run command.
  30. Add GoBin To system path So that executables can be run From any location.
  31. Go fmt formats code To resolve formatting issues during copy and paste.
  32. Go clean cleans/deletes the executables From current directory.
  33. Declaring variables in Go
    1. Var {var_name} string
    2. Local variables have a local scope that is scope is limited to function block only.
    3. Package levels variable have scope within the package.
      1. Package is a directory Where we organise source files.
    4. The keyword var Is used to declare variables
    5. Inside the functions, short assignment operator (:=) Can be used to declare and initialise variables without using var and datatype.
    6. Short assignment operator (:=) Can’t be used for declaring package level variables.
    7. The keyword const is used to declare constant variables.
    8. Go doesn’t provide enum So we have to use some constant mechanism to build enumerate.
    9. Short assignment operator is only needed at the time of declaration. When we reassign value to Variable Defined by short assignments, operator, we should not use the same.
  34. Loops
    1. There is no do while, while loops only for loop is there.
    2. An empty for loop Is an infinite loop.
    3. We can use for loop with only one statement as condition.
    4. We can exit from loop using break, We can skip in a loop using continue.
    5. In switch case Break is not mandatory in each case expression.
    6. There is a Range loop Specifically to iterate over Collection.
    7. There is break And continue via which we can control loops.
  35. We use func keyword to write Functions.
    1. Go functions can return multiple values.
    2. An empty interface in arguments supports any data type.
  36. There is No void keyword
    1. No datatype Needs to be mentioned for void return.
  37. We specify The data types in parenthesis when a function returns multiple values.
  38. We must maintain order while returning values.
  39. For a single data type parenthesis is not required.
  40. Functions can have Variadic parameter by using (“…”) syntax.
    1. Variadic parameter Is used to pass Variadic Number of values.
    2. The last parameter in Go can be a variadic parameter.
    3. We cannot have multiple variadic parameter.
    4. Variadic parameters are data type specific.
    5. In Variadic parameter We can pass many number of parameters.
    6. Variadic parameters are indexed from 0 to n number of values.
    7. For range loop is used to iterate over a variadic parameter.
    8. We can also pass a collection in place of Variadic variable.
    9. It is used when values are optional.
    10. If we pass nothing in a Variadic Number nothing is sent.
  41. Go support anonymous functions.
  42. In Go There is no try catch mechanism. It has a built in data type called as error. This is used to handle errors.
  43. Most functions Latest returned value is an error value. The caller function checks If the error value is NULL or not To check successful execution.
  44. If we do not want to return a value in a case, we can return “-“ underscore. An underscore Is a blank identifier In Go.
    1. This means in place of second identifier We are ignoring the values.
    2. Blank identifiers (“-“) Are ignored by the compiler and compiler shows No error on them.
  45. There are no warnings in Go. There are only compiler error.
  46. Slice is a dynamic array in go.
    1. The difference between variadic parameter And dynamic array Is for dynamic array We should always pass a value.
    2. In a Variadic parameter We can also pass a zero number of values Which we can’t do in a dynamic array.
  47. Functional Go
    1. Functions are values in Go
    2. They can be assigned to a variable
    3. Such an assignment is called as function value.
    4. A closure is a function value that references variables form outside the body.
    5. The function may access and assigned to the reference variables, in this sense, the function is “bound” To the variables.
    6. The logic in a Closure function Is given by Caller function.
    7. Functional programming is based on immutable data structures. No mutable data, no state(or implicit state).
    8. Functions are first class citizen in the go programming language.
    9. Go functions are like values
    10. Go supports higher order functions.
      1. Takes one or more function as parameter’s.
      2. Functions can return functions
    11. Go supports closure a function value that references Variables declared outside its body.
    12. Go is not a functional programming language, but can Leverage its capability.
    13. We can also pass functions as parameters.
  48. Every go source file must start with a package main.
  49. When we are using multiple source files dependent on each other we must specify all of them in a go run command or use "go run ." to run all files.
  50. Go provides three types of data structures to work with collection
    1. Arrays all fixed length collection
      1. Array is a value type data structure.
      2. We can use any type of the type in array. That is it can be user defined, primitive or collection type.
      3. All values in an array are initialised to default values.
      4. Array is fixed length data structure which is its limitation.
    1. Slices a dynamically sized collection
      1. Slice is a dynamic array built on top of normal array.
      2. Slice is a three field data structure
        1. Has a pointer that points to an underlying array
        2. Length specifies the length of an underlying array.
        3. Capacity specifies the capacity of the array.
      3. Slices can be enlarged at any time using built-in function append().
      4. A built-in copy function can be used for creating bigger slices by copying contents from existing slice.
      5. Slice is completely a go specific Data structure not found in other programming languages.
      6. Capacity is an optional property of slice which specifies pre allocated memory block.
      7. Pointer points to an array.
      8. Length is the length of underlying array.
      9. Whenever performance is critical. We use the capacity parameter here allocation becomes quick.
      10. By default, both length and capacity will become same while initialising.
      11. In slice we may not give length while declaration.
      12. A nil slice is a slice which has been declared but not yet initialised.
        1. Capacity is not mandatory while declining/initialising slice.
      13. Slice can be initialised either by using make function or by using literal expression.
        1. We can also specify index-based values while initialising a slice.
      14. We can also pass a variadic parameter in append.
      15. We can also pass another slice in the append method.
      16. Slice represents a sliced part of data where inner bound starts with one.
      17. We can remove the inner and outer bound to get entire data from a slice for example copy_slice=slice[:]
    2. Maps is a collection of key/value pairs.
      1. Go provides a built in map type that implement a hash table.
      2. Map is a collection of Key/value pair, which is an unordered collection.
      3. The built in delete function removes An entry from the map for the given key.
      4. Map provides a faster data revival for a Given key.
      5. To iterate over the contents of a map Use the for range keyword.
      6. A hash table is an unordered collection of key value pairs.
      7. Every element/Memory block is uniquely identified by the key and the memory blocks Which are not in any order.
        1. In array And slice There is an order of memory blocks.
      8. Keys work like indexes
      9. Reading is very fast by specifying unique key We can retrieve The value element in very faster manner.
      10. Map is An unordered collection.
      11. A nil map Is a map which is declared, but not initialised.
      12. All the reference types must be initialised 
        1. Channel can only be initialised using make.
      13. We can only insert data after initialising Our data structures.
      14. For range iterator is used to iterate over a map.
      15. We must provide data structure of both keys and values.
      16. To retrieve Data from map, we can use one variable or 2 variable option.
        1. With one Variable option, if keys exist The corresponding value will be retrieved. otherwise, default value will be received. There is no way to know if the value exist or not.
          1. lan:=langs[“EL”]
        2. With two variable option, which is a better Approach in first variable, we will get the value if the value exist or not. The second variable is a Boolean expression If key exist Boolean value is true else false value.
          1. lan,ok := langs[“EL”]
      17. For Insert and update We use same mechanism. If we overwrite a key That is Reassign value to a key It will be An update expression.
      18. To check existence of a key retrieve it using 2 variable And check Boolean value.
      19. While retrieving values They may not be An order in their, retrieval As it is an is unordered.
      20. Keys are always unique in a map
    3. Channel is another built data structure which is reference type.
    4. Struct(interface) is a user defined datatype which can be a value or a reference.
  51. Slice map and channel or a part of concurrency programming.
  52. Slice and map are reference types, which must be initialised either by using built-in, make function or literal expression.
  53. Slice,map and channel can be initialised using make function. We can initialise Slice and map using literals.
  54. In Go, there is a function called as init()
    1. init is executed automatically before Main()
    2. It is used to initialise package level variables.
  55. In Go, we have literal expression that is using curly brackets {} we can initialise a value.
    1. When we do with array we call it a array literal.
      1. We can specify/initialise values at specific index with array literal.
    2. When we do with Slice we call it a slice literal.
    3. When we do with a Map we call it a map literal.
    4. We use literal assignments with short assignment operator.
    5. We initialise all the values in a single expression.
    6. We can also use a 3 dot("...") operator to specify length of a value. In this case, we do not need to specify numeric length specifically. It automatically takes based on number of elements.
  56. Go provides very limited number of built in functions.
  57. Default value of a string datatype is empty string.
  58. We can use normal for loop to iterate over get data structures but better is to use for range expression.
    1. We can use for range to iterate over only keys, only values or both key and values.
    2. When using for range for only values we must use a blank identifier for keys.
    3. For range can be used to iterate over array, slice, map, channel, variadic parameter.
  59. Any unused variable is considered as a compiler error in go.
  60. We must use a blank identifier in this case for example, while defining keys and values while iterating a list and only using values.
  61. The built in append function check for available capacity. If it is less than required it dynamically. allocates them.
    1. Available capacity is also referred to as pre allocated memory.
  62. To write multi line expressions in Go, use a “,”
  63. References
    1. https://go.dev/
    2. blog.golang.org/gopher
    3. https://github.com/shijuvar/gokit
    4. https://github.com/gauravmatta/golang
    5. https://pkg.go.dev/
    6. https://shijuvar.medium.com/

Comments