Golang Flashcards
What is GO
Statically typed, compiled language. (Code is translated directly into machine code resulting in fast execution and performance)
GO Key Features
- Concurrency
- Garbage collection
- Strong standard library
- Cross platform
GO: Garbage Collection
Automatically manages memory, freeing developers from manual memory management tasks and reducing the risk of memory leaks
GO Standard Library
Includes packages for handling common tasks like I/O operations, web servers, and data serialization
SQLC
Generates type safe GO code from SQL queries
- Works by taking SQL queriers written by dev and converts it to Go code which includes methods to execute those queries
pgx
PostgreSQL driver and toolkit for GO
- PostgreSQL client library for Go
- provides functionalities for low level and high level interactions
- allows you to connect to postgreSQL DB and execute a query
Gin
Web framework in Go used to build high performance HTTP web apps and APIs
- known for its speed and efficiency which can handle large number of requests with minimal overhead
tx
Abbreviation for transaction
- represents an instance of a DB transaction
- allows for grouping multiple DB operations into a single unit of work that can be committed or rolled back as a while
BindJSON
Binds JSON data from an HTTP request body to a GO struct
- it reads the JSON payload from the request and populates the fields of the provided struct with the corresponding data
Key features of Gin
- fast performance
- routing (provides flexible routing system)
- middleware (allows you to execute code before or after request handling)
- JSON handling (binding JSON request payloads to GO structs and rendering JSON responses)
- Error handling
GO Struct
Data type that groups together variables (fields) under a single name
- Each field can have a diff type and are used to represent a collection of related data
- Purpose: represent real world entities by grouping related data
GO Interface
A type that specifies a contract of methods that a type must implement to satisfy the interface. It doesn’t hold data but describes behavior.
- Purpose: defines a set of methods that a type MUST implement allowing diff types to be used interchangeably if they satisfy the interface
How SQLC works
- Write SQL
- Generate code, where go types, structs and query functions are created
- Use in Go
go_struct_tags
referenced in sqlc.yaml file
- Allows you to customize the Go struct tags for fields generated by sqlc
- Control how struct fields are processed when encoding, decoding or interacting with DBs
ex: you might want tags for DB column mapping, JSON serialization etc
Interface
A type that defines a set of methods (behavior) but doesn’t provide the implementation for the methods
- Defines what methods a type must have to satisfy the interface
*gin.Context
in Gin is used to manage HTTP requests and responses, route parameters and middleware in web apps
Parameterized Queries
Technique used when interacting with DBs to prevent SQL injection vulnerabilities
Instead of directly embedding user provided values into SQL query strings placeholders are used.
These placeholders are then replaced with the actual values at runtime by the DB driver
GO modules
A collection of related GO packages that are versioned together as a single unit
standard way to manage dependencies in GO
go.mod
defines the module and its dependencies
- the main config file for go modules
- the first line specifies the module name
ex: like package.json
** manually edited or via go mod commands
go.sum
this file ensures the integrity and security of dependencies
- it contains checksums (hashes) of module versions listed in the go.mod file
ex: like yarn.lock
** automatically managed by go
fmt.Errorf
Function that allows you to create formatted error messages
- Returns an error with the formatted message