Open mtchuyen opened 4 years ago
getopt-like behavior in Go
https://dev.to/ankit01oss/5-reasons-why-go-is-taking-over-devops-in-2021-244
According to 2020 developer survey by Stackoverflow, Go stands at 5th position in the list of of most loved programming languages and at 3rd position in the list of most wanted languages to learn.
Go programs are statically linked
When you compile a Go program, everything is included as a single binary and there are no external dependencies that will need to be installed on the targeted machine that the Go program is intended to run on. And this makes the deployment of Go programs really easy.
Build and deployment of Go programs
Even though Go is a compiled language, it compiles directly into machine code as opposed to intermediate object code which is required for Java. Go was designed to be extremely fast. Here’s a small benchmark game comparing Go vs Python.
Go is platform independent
Binary executables of Go programs can be produced for any platform like Windows, Linux and macOS. In order for a binary to be supported on the different operating systems all it takes is setting two environment variables: $GOOS, $GOARCH.
Extremely performant language
Go program has fast compilation times and fast runtimes with lower resource usage like CPU and memory especially when compared to language like Python. As the compiler fails the run if there are unused imports, the compilation time is short. And as the code is compiled to machine code, it also executes fast.
Availability of Standard libraries
Unlike Python which often requires the use of third party libraries to implement a particular Python program, the Go programming language has a standard library which includes a lot of built-in functionality that you will need as a DevOps engineer. This includes functionality like file processing, HTTP web services, JSON processing, native support for concurrency and parallelism as well as built-in testing modules.
https://medium.com/codex/advantages-and-disadvantages-of-golang-go-fb09de9333ae
Bởi vì Java đã có lịch sử lâu đời, nên việc optimize code tốt hơn Go (tới 2020). Nhưng có 1 số vấn đề trong sử dụng có thể Go thuận tiện hơn:
https://medium.com/@mohammed.benthaier/whats-so-cool-about-golang-e3bc6632c6db
https://spiralscout.com/blog/when-to-use-go-vs-java-one-programmers-take-on-two-top-languages
https://codetoanbug.com/so-sanh-giua-c-va-golang/
Mặc dù Go không phải là ngôn ngữ hướng đối tượng OOP, nhưng những người làm ra Golang lại là những chuyên gia, vì vậy họ vẫn phát triển Golang theo những tiêu chuẩn nhất định (vô tình hay hữu ý).
Khả năng return
của 1 hàm func
trong Go dù 1 giá trị hay nhiều giá trị thì vẫn tuân thủ theo nguyên lý Single-Responsibility Principle
SOLID is an acronym for the first five object-oriented design (OOD) principles by Robert C. Martin (also known as Uncle Bob). SOLID stands for:
S - Single-responsiblity Principle O - Open-closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D - Dependency Inversion Principle
Việc nhiều giá trị được Return
và assign
mà chỉ cần cách nhau bởi dấu phẩy (,
- comma) khiến cho việc sử dụng rất dễ dàng.
func myValues() (int, string) {
return 3, "Hi!"
}
Cái này rất hay, nhiều khi bạn chẳng muốn quan tâm tới cái giá trị nào đó được trả về, mà cứ mất tài nguyên cho việc khai báo nó, vậy thì Go đưa ra khái niệm Blank Identifier
giúp chương trình của bạn đếc quan tâm
tới nó nữa.
_, c := myValues()
Dân lập trình nổi tiếng là lười, cho nên với những build-in func thì mấy cái biến thích nhất ngắn nhất có thể. Vậy nên Go KHÔNG có quy ước đặt tên biến
se := rand.NewSource(time.Now().UnixNano())
r := rand.New(se)
se
, r
đều là ngắn nhất có thể, thế cho nhanh...
Cho phiên bản 1.15.x
thì code này vẫn thấy cho phép chạy
package main
import "fmt"
var true = false
func main() {
var status bool = true
if status {
fmt.Println("True")
} else {
fmt.Println("False")
}
}
Vâng, nó in ra là:
False
Dự án Go mobile đang phát triển để viết app trên các thiết bị di động (nền tảng Android và iOS)
Golang có thể dùng để cung cấp dịch vụ Webserver, nhưng những thứ hiển thị ở phía trên client thì vẫn phải dùng ngôn ngữ khác (JavaScript). Nhưng giờ Golang cung cấp khả năng WebAssembly (feature can execute code written in any programming language on browsers)
Chúng ta có thể compile Golang to WASM via the official Go compiler
or TinyGo.
GOOS=js GOARCH=wasm go build -o hello.wasm hello.go TinyGo giúp giảm size của WASM package
Bạn cũng có thể chuyển đổi các action viết bằng Golang sang JavaScript bằng dự án GopherJS
import "syscall/js"
js.Global().Call("alert", "Hello Gophers!")
GopherJS community also created Go bindings for popular JavaScript libraries/frameworks such as React.js, Vue.js, Angular.js, Electron, etc. Now you can build web application frontends and heavy data processing programs for browsers using Go.
Go+ (formerly qlang) offers impressive features for data processing/data science and scientific, such as:
Go+, tfgo (Tensorflow Go bindings), and Gonum (a NumPy-like Go library) make a promising future for Go-powered data science.
Kể ra mấy đặc tính này giúp Go có thể nói là dẫn đầu trong việc DevOps:
The Lorca project lets you write desktop applications with Go and JavaScript. Also, it’s possible to write Lorca apps entirely in Go by combining GopherJS with Lorca. Lorca uses your already installed Chromium browser as the web renderer and exports Go functions to the browser context via the Chrome DevTools WebSocket protocol.
https://pavledjuric.medium.com/if-you-are-learning-golang-remember-these-10-commands-44e3bc194b16
go version
go version go1.17.6 linux/amd64
go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/bin"
GOCACHE="/home/.cache/go-build"
GOENV="/home/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd6
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/src/
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-le
go mod init <project name>
It will initialize a go.mod file, which is something similar to a pom.xml if you are coming from Java, or package.json if you are coming from JavaScript.
go get <package name>
install a certain third party library or framework which you will use in your project
go run .
go build -o <name of binary> <packages>
go fmt
format your code before merging
go mod tidy
remove all unused modules from your go.mod file, so you don’t have to manually edit this file.
go test
run your tests simply
go vet
Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.
Comparing Python, Java and Go performance
https://medium.com/@rodrigo.javier.ramirez/compiled-vs-interpreted-code-performance-e1a63299760b