1. Background
For the past decade or so, C/C++ has not developed well in the computing field and has not kept pace with the advance of computers.
With existing programming languages, development speed and system efficiency often cannot be had at the same time. Either execution is fast but development and compilation are inefficient, or execution is slow but compilation is efficient. So a programming language was needed with relatively fast execution, compilation, and development alike — and Go emerged out of nowhere.
The main goal of Go is to organically combine the safety and efficiency of a statically typed language with the ease of development of a dynamically typed one.
Another goal is support for network communication, concurrency, and parallel programming, so as to make better use of the many distributed and multi-core computers.
2. Introduction to Go
Go is an open source project released by Google, and it is a systems development language.
The initial design of Go was begun by Robert Griesemer, Rob Pike, and Ken Thompson in September 2007, and the official release was in November 2009. In May 2010, Rob Pike publicly announced that it was being used in a backend system inside Google.
Go can run on Linux, Mac OS X, FreeBSD, OpenBSD, Plan 9, and Windows, and it also supports several processor architectures: I386, AMD64, and ARM.
Because Go is optimized for multi-CPU systems, programs written in Go can match C in execution efficiency while being safer and supporting parallelism.
3. Advantages
- Simple deployment
What Go compiles into is a static executable, with no external dependency other than glibc. - Good concurrency
Goroutines and channels make writing highly concurrent server-side software very easy. In many cases there is no need to think about locking mechanisms at all. Go’s concurrency is not provided as a library; support for concurrency is built into the language itself. - Programmer-friendly
Compared with C/C++, Go does not support some advanced syntax, its code is more concise, and it has some of the features of a dynamic language. - Good execution performance
Execution efficiency is high — not as high as C/C++, but far higher than Python, and memory usage is very efficient too.
4. Basic Concepts and Syntax of Go
- File names
Go source files are stored on the computer with the .go suffix, and these file names consist of lowercase letters, such as scanner.go . If a file name is made up of multiple parts, they are separated with an underscore _ , such as scanner_test.go - Packages
A package is a way of structuring code: every program is made up of packages (usually abbreviated pkg), and you can use your own packages or import content from other packages. The first line of a source file states which package the file belongs to, for example: package main, which indicates a program that can be executed on its own. - Constants
Constants are defined with the const keyword and are used to store data that will not change. The format for defining a constant is: const identifier [type] = value. - Variables
The general form for declaring a variable uses the var keyword: var identifier type. - Data types
18 basic types — bool、string、rune、byte、int、uint、int8、uint8、int16、uint16、int32、uint32、int64、uint64、float32、float64、complex64、complex128 — and 7 composite types: array、struct、function、interface、slice、map、channel - Control structures
Go provides the if-else structure, the switch structure, the select structure, and the for (range) structure. - Function definition
| |
5. Built-in Standard Library
- fmt
The fmt package implements formatted I/O functions, similar to printf and scanf in C, but simpler. - strings
The strings package implements simple functions for manipulating strings, including the exported functions of strings and the two structs Reader and Replacer. - time
The time package provides functions for displaying and calculating time. - os
The os package provides a platform-independent interface to operating system functions. Error handling is designed in Go style: a failed call returns an error value rather than an error code. Usually the error value contains more information. - path
path implements functions for manipulating slash-separated paths. - io
The io package provides basic interfaces to I/O primitives. These interfaces abstract out generally useful functions and add some operations on top. Because these interfaces wrap low-level operations whose underlying implementations differ completely, clients should not assume they are concurrency-safe unless told otherwise. - log
The log package implements a simple logging service. The log package defines the Logger type, which provides some methods for formatted output. It also provides a predefined standard Logger, accessible through the helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which is easier to use than creating a Logger object by hand. The Logger prints the date and time of each log message, and by default outputs to standard error. The Fatal series of functions calls os.Exit(1) after writing the log message. The Panic series of functions calls panic after writing the log message.
6. Hello World
6.1 Environment Setup
Recommended IDE: JetBrains Gogland, or any editor with a Go plugin, such as: Atom + go-plus
Taking installation on CentOS as an example:
| |
View the Go environment configuration
| |
GOARCH="amd64"
GOOS="linux"
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix- map=/tmp/go-build157488013=/tmp/go-build -gno-record-gcc-switches"
CGO_LDFLAGS="-g -O2"
6.2 Hello
The hello.go file
| |
Run the code
| |
