Language
-
Unused
imports
andvariables
cause compiler error. -
Inside a function, the
:=
short assignment statement can be used in place of avar
declaration with implicit type. -
Constants
cannot be declared using the:=
syntax. -
switch
- No
break
statement needed at the end of each case case
can have boolean expression instead of a literal value, similar toif-else
structure
- No
-
for
- Using
range
to iterate array
- Using
-
new
andmake
make
returns an initialized (not zeroed) value of typeT
.make
applies only tomaps
,slices
andchannels
and does not return a pointer.
Function
- No overloading
defer
- Similar to Java's
finally
block, 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.ReentrantLock
java.util.concurrent.locks.ReentrantReadWriteLock
-
But mutexes in
Go
aren’treentrant
. If agoroutine
tries to acquire the same lock twice, it deadlocks, waiting for itself to release the lock. -
Like
sync.WaitGroup
andsync.Once
,mutexs
must 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 amutex
is copied, its lock won’t be shared.
Goroutine
-
If main function ends, all goroutines will be terminated too.
-
select
statement-
The
select
statement lets agoroutine
wait on multiple communication operations. -
A
select
blocks until one of itscases
can run, then it executes thatcase
. It chooses one at random if multiple are ready. -
To avoid unexpected blocking, a
default
case should be supplied.
-
Channel
- Use
Channel
for synchronous data exchange betweengoroutines
, similar to a JavaBlockingQueue
- Buffered
Channel
can be used to implement a Semaphore control for concurrency
Engineering
Go Modules
- Both
go.mod
andgo.sum
should be checked into version control.
List all dependencies of the current module
go list -m all
Workspace && Version Control
GOPATH
should point to a directory where allgolang
project 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)