Language
-
Unused
importsandvariablescause compiler error. -
Inside a function, the
:=short assignment statement can be used in place of avardeclaration with implicit type. -
Constantscannot be declared using the:=syntax. -
switch- No
breakstatement needed at the end of each case casecan have boolean expression instead of a literal value, similar toif-elsestructure
- No
-
for- Using
rangeto iterate array
- Using
-
newandmakemakereturns an initialized (not zeroed) value of typeT.makeapplies only tomaps,slicesandchannelsand does not return a pointer.
Function
- No overloading
defer
- Similar to Java's
finallyblock, but unstructured, and can be used for a simple method call or a closure, and doesn't have to be at the end of the method.
Array & Slice
- When you pass variables between functions, they’re always passed by value, and this means the entire array, regardless of its size, is copied and passed to the function.
Slice's length cannot exceed its capacity.- If you specify a value inside the
[ ]operator, you’re creating anarray. If you don’t specify a value, you’re creating aslice.
Concurrency
sync.Waitgroup
-
Use case
- Have main function wait for all goroutines finishing execution
-
Similar to Java's
java.util.concurrent.CountDownLatch
sync.Once
-
Use case
- Can be used to run initialization action that must be finished before any goroutine can proceed.
sync.Mutex & sync.RWMutex
-
Similar to Java's
java.util.concurrent.locks.ReentrantLockjava.util.concurrent.locks.ReentrantReadWriteLock
-
But mutexes in
Goaren’treentrant. If agoroutinetries to acquire the same lock twice, it deadlocks, waiting for itself to release the lock. -
Like
sync.WaitGroupandsync.Once,mutexsmust never be copied. If they are passed to a function or accessed as a field on a struct, it must be via a pointer. If amutexis copied, its lock won’t be shared.
Goroutine
-
If main function ends, all goroutines will be terminated too.
-
selectstatement-
The
selectstatement lets agoroutinewait on multiple communication operations. -
A
selectblocks until one of itscasescan run, then it executes thatcase. It chooses one at random if multiple are ready. -
To avoid unexpected blocking, a
defaultcase should be supplied.
-
Channel
- Use
Channelfor synchronous data exchange betweengoroutines, similar to a JavaBlockingQueue - Buffered
Channelcan be used to implement a Semaphore control for concurrency
Engineering
Go Modules
- Both
go.modandgo.sumshould be checked into version control.
List all dependencies of the current module
go list -m all
Workspace && Version Control
GOPATHshould point to a directory where allgolangproject reside.
GOPATH/
bin/ # Build artifacts
hello # command executable
outyet # command executable
src/
golang.org/x/example/ # Project 1 (Git repo 1)
golang.org/x/image/ # Project 2 (Git repo 2)-
Resources
- The Go Programming Language - How to Write Go Code (with GOPATH) (opens in a new tab)
- GoLand - GOROOT and GOPATH (opens in a new tab)
- JFrog Artifactory - Choosing Your GOPROXY for Go Modules (opens in a new tab)
- GitHub - goproxy.io - GOPRIVATE Environment (opens in a new tab)
- GitHub - goproxy.io - GOSUMDB Environment (opens in a new tab)