type
status
date
slug
summary
tags
category
icon
password
Prelude:
No need for digging a big hole, just use it.
Golang in short
Go is
- A modern high-level programming language that resembles C
- Open-source, compiled, statically typed, and memory-safe
Go has
- Simple yet powerful package management (pulls directly from GitHub)
- Built-in concurrency primitives
- Garbage collection (GC)
- Lightning-fast compilation
- Minimal syntax
- Cross-platform compilation
Go doesn't have
- Classes and inheritance
- Function overloading
- Implicit type conversions
I use Go to
- Say goodbye to Java's verbose syntax
- Build web services and microservices
- Handle high-concurrency scenarios
Go is perfect for
- Web services and RESTful APIs
- Microservices architecture
- Network programming and distributed systems
Go might not be ideal for
- GUI desktop applications
- Kernel development
- Machine learning (Python has a stronger ecosystem)
Installing Go
- Using OS package managers
- Linux:
apt
,yum
,snap
- macOS:
brew
- Windows:
choco
,scoop
, etc.
- Download official precompiled binaries
- Already compiled, ready to use
- Set up path and links manually
- Verify with
go version
Hello World
Types
Variables and Functions
Arrays, Slices, and Maps
Control Flow
Defer and Channels
Pointers
Format Verbs
Verb | Description | Example | Output |
%v (value) | Default format | Printf("%v", people) | {zhangsan} |
%+v | Adds field names for structs | Printf("%+v", people) | {Name:zhangsan} |
%#v | Go syntax representation | Printf("%#v", people) | main.Human{Name:"zhangsan"} |
%T (type) | Type in Go syntax | Printf("%T", people) | main.Human |
%% | Percent sign | Printf("%%") | % |
%t (true) | true or false | Printf("%t", true) | true |
%b (binary) | Binary representation | Printf("%b", 5) | 101 |
%c (char) | Unicode character | Printf("%c", 0x4E2D) | 中 |
%d (decimal) | Decimal | Printf("%d", 0x12) | 18 |
%o (octal) | Octal | Printf("%o", 10) | 12 |
%q (quote) | Single-quoted character literal | Printf("%q", 0x4E2D) | '中' |
%x | Hexadecimal, lowercase | Printf("%x", 13) | d |
%X | Hexadecimal, uppercase | Printf("%X", 13) | D |
%U (unicode) | Unicode format: U+1234 | Printf("%U", 0x4E2D) | U+4E2D |
%b | Binary exponent scientific notation | Printf("%b", 10.5) | 5835037194198p-49 |
%e | Scientific notation | Printf("%e", 10.2) | 1.020000e+01 |
%E | Scientific notation | Printf("%E", 10.2) | 1.020000E+01 |
%f (float) | Decimal point, no exponent | Printf("%f", 10.2) | 10.200000 |
%g | Compact format (%e or %f) | Printf("%g", 10.20) | 10.2 |
%G | Compact format (%E or %f) | Printf("%G", 10.20) | 10.2 |
%s (string) | String (string or []byte) | Printf("%s", []byte("Go")) | Go |
%q | Double-quoted string | Printf("%q", "Go") | "Go" |
%x | Hex, lowercase, two chars per byte | Printf("%x", "golang") | 676f6c616e67 |
%X | Hex, uppercase, two chars per byte | Printf("%X", "golang") | 676F6C616E67 |
%p (pointer) | Hexadecimal with 0x prefix | Printf("%p", &people) | 0x4f57f0 |
+ | Always print sign; ASCII-only for %+q | Printf("%+q", "中文") | "\u4e2d\u6587" |
- | Pad with spaces on right (left-align) | ㅤ | ㅤ |
# | Alternate format: 0 prefix for octal (%#o), 0x for hex (%#x), etc. | Printf("%#U", '中') | U+4E2D '中' |
' ' | Space for elided sign; spaces between bytes for hex | ㅤ | ㅤ |
0 | Pad with zeros; moves padding after sign for numbers | ㅤ | ㅤ |
Reference:
String Formatting
Structs
Interfaces
Error Handling
Concurrency
Various Magic 🪄
...
Notes:
- Variadic parameter must be last
- Inside function, variadic parameter is slice type
- Types must match when expanding
Initialization Tricks
iota Enumerations
Type Aliases vs Type Definitions
make vs new
Go Gotchas and Important Notes
Slice Underlying Array Sharing
Map Randomness
String Bytes vs Characters
Interface nil Pitfall
Loop Variable Pitfall
Method Call Auto-conversion
Struct Embedding (Anonymous Fields)
init Functions and Package Imports
- Author:Parker Chen
- URL:www.parkerchenca.com/article/243f0ccf-d7f8-8072-92d1-e14fca001dac
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts