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 asnil
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
orparent
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
orbig.NewInt
.
š Date & Time Formatting
time.Now().Format("01-02-2006 15:04:05 Monday")
š Error Handling
- Use the
fmt
package instead ofprint
andpanic
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.
- Use
š Channels for Communication
- Basic Syntax:
- Sending data:
ch <- value
- Receiving data:
value := <-ch
- Sending data:
- 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