mtchuyen / Golang-Tips

Tips in Golang programming
3 stars 2 forks source link

Compare Golang with others #2

Open mtchuyen opened 4 years ago

mtchuyen commented 4 years ago

Comparing Python, Java and Go performance

https://medium.com/@rodrigo.javier.ramirez/compiled-vs-interpreted-code-performance-e1a63299760b

mtchuyen commented 3 years ago

getopt-like behavior in Go

https://newbedev.com/getopt-like-behavior-in-go

mtchuyen commented 2 years ago

5 reasons why Go is taking over DevOps in 2021?

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.

mtchuyen commented 2 years ago

Advantages And Disadvantages Of Golang (Go)

https://medium.com/codex/advantages-and-disadvantages-of-golang-go-fb09de9333ae

mtchuyen commented 2 years ago

Is Go faster than Java in some use cases?

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:

  1. Start up/Down faster. Tôi có sử dụng script (bash) để restart 1 service, app Java thường có lỗi nhiều hơn app Go.
  2. Low level IO. Có vẻ Go nhanh hơn và thuận tiện hơn Java. Với Java, có thể là do chính sách phát triển. Với Go thì có ưu điểm là Dev ít phải quan tâm hơn tới việc cân bằng IO với CPU.
  3. Garbage collection. Khả năng optimized GC của Java và Go ngang ngửa nhau. Java dùng nhiều tài nguyên hơn để collect, Go thì chặt chẽ hơn. Cần phải test với các trường hợp cấp phát heap lên tới đơn vị GB, xem khả năng GC cua Go và Java thế nào, có vẻ Go đang gặp khó trong vấn đề này.
mtchuyen commented 2 years ago

What’s so cool about golang

https://medium.com/@mohammed.benthaier/whats-so-cool-about-golang-e3bc6632c6db

mtchuyen commented 2 years ago

When to Use Go vs. Java

https://spiralscout.com/blog/when-to-use-go-vs-java-one-programmers-take-on-two-top-languages

Go vs C++ (Cplus)

https://codetoanbug.com/so-sanh-giua-c-va-golang/

Go vs Python

mtchuyen commented 2 years ago

Những điều thích thú của Go

Return multi value

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 Returnassign 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!"
}

Underscore ”_” Blank Identifier

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()

Quy ước đặt tên biến

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...

Cẩn thận với cách đặt tên biến

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

mtchuyen commented 2 years ago

Go có thể làm gì?

1. Mobile Apps

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)

2. Running Go On Web Browsers

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)

3. Golang cũng có thể làm Data science

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.

4. Using Go for Automation and DevOps

Kể ra mấy đặc tính này giúp Go có thể nói là dẫn đầu trong việc DevOps:

5. Go cũng có thể lập trình cho hệ thống nhúng - Embedded Systems

6. Building Hybrid Desktop Apps with Go

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.

mtchuyen commented 1 year ago

Những câu lệnh hay dùng trong Golang

https://pavledjuric.medium.com/if-you-are-learning-golang-remember-these-10-commands-44e3bc194b16

  1. go version

go version go1.17.6 linux/amd64

  1. 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
  1. 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.

  1. go get <package name>

install a certain third party library or framework which you will use in your project

  1. go run .

  2. go build -o <name of binary> <packages>

  3. go fmt

format your code before merging

  1. go mod tidy

remove all unused modules from your go.mod file, so you don’t have to manually edit this file.

  1. go test

run your tests simply

  1. 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.