Categorygithub.com/anideep-kalia/go-lang
module
0.0.0-20250107211346-016aeb2b4efa
Repository: https://github.com/anideep-kalia/go-lang.git
Documentation: pkg.go.dev

# README

GoLang Quick Reference Guide 🐹

šŸ“š General Concepts

Null and Nil

  • null is referred to as nil in Go.
  • Data type descriptions in Go come after the variable name, unlike some other languages.

No Inheritance

  • Go does not support inheritance, nor does it have super or parent keywords.

šŸ“„ File Handling in Go

  • You can have multiple files in the same package using the same main function.
  • If a conflict occurs, adjust by changing one main function and calling it within another.

šŸ–Øļø Print Functions

  • fmt.Printf requires format specifiers such as %T (type) or %v (value) to display variables.
  • fmt.Println displays variables directly without requiring format specifiers.

šŸ”¤ Data Types and Strings

  • All input data in Go is treated as string.
  • Handling large numbers (64-bit and above) often requires packages like big.Int or big.NewInt.

šŸ•’ Date & Time Formatting

time.Now().Format("01-02-2006 15:04:05 Monday")

šŸ›‘ Error Handling

  • Use the fmt package instead of print and panic for better formatting and error management.

🧩 Working with Arrays and Slices

  • fruits2[:3] retrieves the first 3 elements of an array.
  • Use make for creating slices, maps, and channels in Go.

šŸ“¦ Maps and Structs

  • Map Declaration Example:

    var myOnlineData map[string]interface{}
    

    Here, interface{} allows mapping of any data type.

  • Difference in println with Structs:

    • %v shows the struct value.
    • %+v shows struct fields and values.

šŸ”€ Control Flow

  • Go supports the goto command to jump between code blocks.
  • The defer keyword stores the deferred code, executing it in reverse order.

šŸš€ Goroutines and Concurrency

  • Goroutines: Lightweight threads managed by Go runtime for concurrent execution.
  • Example of Starting a Goroutine:
    go functionName()
    
  • WaitGroups (sync.WaitGroup):
    • Use wg.Add() to increase thread count.
    • Use wg.Done() to decrease it.

šŸ”— Channels for Communication

  • Basic Syntax:
    • Sending data: ch <- value
    • Receiving data: value := <-ch
  • Channels operate like stacks; data is removed after being retrieved.
  • Closing a channel disallows further data pushes.
  • Control Concurrency
    • These are also used to control number of concurrency taking place i.e. number of goroutines working at a time
    • EXAMPLE:
      • Number of Goroutine is controlled by fixing the size of channel
      • Each goroutine pushes in stack before starting but if the channel is full
      • Then that goroutine has to wait befoer channel is freed; else if the channel has space
      • Then goroutine don't have to wait and can start immediately after pushing into channel

šŸ“¦ Modules and Packages

  • Local Setup:
    • go mod init {folder_name}: Makes the folder's contents available locally.
  • Remote Setup:
    • go mod init github.com/username/{folder_name}: Publishes to GitHub for broader access and Makes the folder's contents available locally.

āœļø Syntax Quick Reference

Variables

var {name} int  // Declaration
{name} := value // Short-hand assignment

Maps

{name} := make(map[int]int)

Structs

type User struct{}

Packages

All files in the same folder belong to the same package. You do not need to import files within the same package explicitly. Example:

project/
ā”œā”€ā”€ main.go
ā”œā”€ā”€ helpers/
│   ā”œā”€ā”€ math.go
│   ā”œā”€ā”€ string.go
  • helpers/math.go:

    package helpers
    
    func Add(a, b int) int {
        return a + b
    }
    
  • helpers/string.go:

    package helpers
    
    func ToUpperCase(s string) string {
        return strings.ToUpper(s)
    }
    
  • main.go:

    package main
    
    import (
        "project/helpers"
        "fmt"
    )
    
    func main() {
        fmt.Println(helpers.Add(3, 5))          // Output: 8
        fmt.Println(helpers.ToUpperCase("go")) // Output: GO
    }
    

here project is initialised directory during

  go mod init project

but if you used github URL then you would import these folder as

  import (
    "github.com/username/{folder_name}/helpers"
  )

above in main folder


Additional Notes šŸ“

  • Anonymous Functions: Define and call immediately:
    func() { ... }()
    
  • Ensure synchronization when using Goroutines by using WaitGroups, or else the main function may exit prematurely.
  • Miscellaneous
    type DefaultParser struct {
      customField string
    }
    func (d DefaultParser) GetCustomField() string {   // copy of the instance and not actual instance will be changed
      return d.customField
    }
    func (*d DefaultParser) GetCustomField() string {   // actual instance will be modified in this function
      return d.customField
    }
    

Receiver

(r *mutationResolver) -> receiver to show that CreateAccount is a method of mutationResolver just like in OOP

  func (r *mutationResolver) CreateAccount(ctx context.Context, in AccountInput) (*Account, error) {

# Packages

No description provided by the author