日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

go语言简述

發(fā)布時(shí)間:2024/6/21 综合教程 19 生活家
生活随笔 收集整理的這篇文章主要介紹了 go语言简述 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

0. 簡(jiǎn)述

Go是一個(gè)開(kāi)源的編程語(yǔ)言,它能讓構(gòu)造簡(jiǎn)單、可靠且高效的軟件變得容易。

Go語(yǔ)言被設(shè)計(jì)成一門(mén)應(yīng)用于搭建web服務(wù)器,存儲(chǔ)集群或類(lèi)似用途的巨型中央服務(wù)器的系統(tǒng)編程語(yǔ)言。對(duì)于高性能分布式系統(tǒng)領(lǐng)域而言,Go語(yǔ)言無(wú)疑比大多數(shù)其他語(yǔ)言有著更高的開(kāi)發(fā)效率。它提供了海量并行的支持,這對(duì)于游戲服務(wù)器端的開(kāi)發(fā)而言是再好不過(guò)的。

Go官網(wǎng):https://golang.google.cn/或https://github.com/golang/go

package main
import "fmt"
func main() {
    fmt.Println("Hello, World!")
}

X86上編譯運(yùn)行:

wang@ubuntu:~/go$ go build -o hello hello.go
wang@ubuntu:~/go$ ./hello
Hello, World!

或直接運(yùn)行

wang@ubuntu:~/go$ go run hello.go
Hello, World!

X86平臺(tái)交叉編譯ARM64平臺(tái)上程序:

GOOS=linux GOARCH=arm64 go build -o hello hello.go
wang@ubuntu:~/go$ file hello
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, not stripped

godoc用法:

 godoc -http=localhost:6060 -play

godoc可提供離線(xiàn)的go文檔,便于本地查看。

可在https://godoc.org/中搜索所有g(shù)olang庫(kù)的接口說(shuō)明。

1. Go編譯器

兩種官方編譯器,gc和gccgo,其中g(shù)ccgo基于gcc后端。

go編譯器支持8種指令集,不同建構(gòu)編譯質(zhì)量不同:

amd64 (also known as x86-64)    
386 (x86 or x86-32)   Comparable to the amd64 port.
arm (ARM)
Supports Linux, FreeBSD, NetBSD, OpenBSD and Darwin binaries. Less widely used than the other ports.
arm64 (AArch64)
Supports Linux and Darwin binaries. New in 1.5 and not as well exercised as other ports.
ppc64, ppc64le (64-bit PowerPC big- and little-endian)
Supports Linux binaries. New in 1.5 and not as well exercised as other ports.
mips, mipsle (32-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.8 and not as well exercised as other ports.
mips64, mips64le (64-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.6 and not as well exercised as other ports.
s390x (IBM System z)
Supports Linux binaries. New in 1.7 and not as well exercised as other ports.

go編譯環(huán)境可以被定制,與平臺(tái)和建構(gòu)相關(guān)的是$GOOS和$GOARCH,分別指定目標(biāo)操作系統(tǒng)和目標(biāo)建構(gòu)。常用組合如下:(注:$GOOS是darwin for macOS 10.1及以上和iOS)

$GOOS        $GOARCH
android     arm
darwin      386
darwin      amd64
darwin      arm
darwin      arm64
linux       386
linux       amd64
linux       arm
linux       arm64
windows     386
windows     amd64

go編譯器(或go環(huán)境)安裝

有兩種安裝方式:二進(jìn)制發(fā)布包和源碼包,參考https://golang.google.cn/doc/install。一般情況下可直接下載二進(jìn)制發(fā)布包,官方已提供了常用平臺(tái)的二進(jìn)制發(fā)布包。

下載二進(jìn)制tar包,tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz,一般安裝路徑為/usr/local,go工具命令要執(zhí)行需要將/usr/local/go/bin導(dǎo)出到PATH環(huán)境變量中(/etc/profile可長(zhǎng)期有效)。

源碼包安裝方式參考:Installing Go from source

ubuntu下可直接apt安裝:

sudo apt-get install golang-go

go工具卸載

linux下直接刪除/usr/local/go目錄即可(同時(shí)修改PATH環(huán)境變量,或/etc/profile)。

GO環(huán)境變量

go env可打印go環(huán)境變量。

$GOPATH

GOPATH指定workspace位置,默認(rèn)為$home/go,go項(xiàng)目在本地的開(kāi)發(fā)環(huán)境的項(xiàng)目根路徑(以便項(xiàng)目編譯,go build, go install,go get)。若工作在其他目錄,需設(shè)定GOPATH。export GOPATH=$HOME/go

或直接寫(xiě)到/etc/profile中,然后source /etc/profile

注意GOPATH不能和go安裝目錄相同。

go env GOPATH

打印當(dāng)前有效的GOPATH,若沒(méi)有設(shè)置打印默認(rèn)位置。

For convenience, add the workspace'sbinsubdirectory to yourPATH:

$ export PATH=$PATH:$(go env GOPATH)/bin

GOPATH之下主要包含三個(gè)目錄:bin,pkg,src。bin目錄主要存放可執(zhí)行文件;pkg目錄存放編譯好的庫(kù)文件,主要是*.a文件;src目錄下主要存放go的源文件。

$GOROOT

go的安裝目錄,配置后不會(huì)更改。一般為/usr/local/go或/usr/go或/usr/lib/go。

$GOROOT_FINAL

$GOOS and $GOARCH

用于不同平臺(tái)的交叉編譯,只需要在build之前設(shè)置這兩個(gè)變量即可,這也是go語(yǔ)言的優(yōu)勢(shì)之一:可以編譯生成跨平臺(tái)運(yùn)行的可執(zhí)行文件。

注意:這個(gè)交叉編譯暫不支持cgo方式,因此交叉編譯時(shí)需要設(shè)置$CGO_ENABLED設(shè)置為0。

$GOHOSTOS and $GOHOSTARCH

$GOBIN

go二進(jìn)制文件安裝目錄,默認(rèn)為$GOROOT/bin。

$GO386

$GOARM

$GOMIPS

集成開(kāi)發(fā)環(huán)境IDE

vscode-golang配置參考VS code golang 開(kāi)發(fā)環(huán)境搭建

2. cgo

Cgo lets Go packages call C code.

The basics

If a Go source file imports"C", it is using cgo. The Go file will have access to anything appearing in the comment immediately preceding the lineimport "C", and will be linked against all other cgo comments in other Go files, and all C files included in the build process.

Note that there must be no blank lines in between the cgo comment and the import statement.

To access a symbol originating from the C side, use the package nameC. That is, if you want to call the C functionprintf()from Go code, you writeC.printf(). Since variable argument methods like printf aren't supported yet (issue975), we will wrap it in the C method "myprint":

package main

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
    printf("%s", s);
}
*/
import "C"

import "unsafe"

func main() {
    cs := C.CString("Hello from stdio
")
    C.myprint(cs)
    C.free(unsafe.Pointer(cs))
}

參考:

1. http://golang.org/doc/articles/c_go_cgo.html

2.https://github.com/golang/go/wiki/cgo

3.http://wiki.jikexueyuan.com/project/go-command-tutorial/0.13.html 極客學(xué)院go命令詳解

4. https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/readme.htmlGo語(yǔ)言高級(jí)編程(Advanced Go Programming)cgo編程

3. Go交叉編譯

參考:Go cross compilation

If cgo is not required (common go programs, not including c/c++)

The go tool won’t require any bootstrapping if cgo is not required. That allows you to target the following program to any GOOS/GOARCH without requiring you to do any additional work. Invokego build.

$ cat main.go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

In order to target android/arm, run the following command.

$ GOOS=android GOARCH=arm GOARM=7 go build .

The produced binary is targeting ARMv7 processors that runs Android. All possible GOOS and GOARCH values are listed on theenvironment docs.

If cgo is required (including c/c++)

If you need to have cgo enabled, the go tool allows you to provide custom C and C++ compilers via CC and CXX environment variables.

$ CGO_ENABLED=1 CC=android-armeabi-gcc CXX=android-armeabi-g++ 
    GOOS=android GOARCH=arm GOARM=7 go build .

The toolchain will invoke android-armeabi-gcc and android-armeabi-g++ if it is required to compile any part of the package with a C or C++ compiler. Consider the following program with a slightly different main function. Rather than outputting “hello world” to the standard I/O, it will use Android system libraries to write “hello world” to the system log.

$ cat main.go
// +build android

package main

// #cgo LDFLAGS: -llog
//
// #include <android/log.h>
//
// void hello() {
//   __android_log_print(
//     ANDROID_LOG_INFO, "MyProgram", "hello world");
// }
import "C"
func main() {
    C.hello()
}

If you build the program with the command above and examine the build with -x, you can observe that cgo is delegating the C compilation to arm-linux-androideabi-gcc.

$ CGO_ENABLED=1 
CC=arm-linux-androideabi-gcc 
CXX=arm-linux-androideabi-g++ 
GOOS=android GOARCH=arm GOARM=7 go build -x .
...
CGO_LDFLAGS=”-g” “-O2” “-llog” /Users/jbd/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/rakyll/hello/_obj/ -importpath github.com/rakyll/hello — -I $WORK/github.com/rakyll/hello/_obj/ main.go
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -print-libgcc-file-name
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -I $WORK/github.com/rakyll/hello/_obj/ -g -O2 -o $WORK/github.com/rakyll/hello/_obj/_cgo_main.o -c $WORK/github.com/rakyll/hello/_obj/_cgo_main.c
...

Pre-building the standard library

The go tool also provides a utility if you would like to pre-build the standard library, targeting a specific GOOS and GOARCH.

$ CGO_ENABLED=1 
    CC=arm-linux-androideabi-gcc 
    CXX=arm-linux-androideabi-g++ 
    GOOS=android GOARCH=arm GOARM=7 go install std

The standard library targeting android/armv7 will be available at $GOROOT/pkg/android_arm.

$ ls $GOROOT/pkg/android_arm
archive    fmt.a      math       runtime.a
bufio.a    go         math.a     sort.a
bytes.a    hash       mime       strconv.a
compress   hash.a     mime.a     strings.a
container  html       net        sync
crypto     html.a     net.a      sync.a
crypto.a   image      os         syscall.a
database   image.a    os.a       testing
debug      index      path       testing.a
encoding   internal   path.a     text
encoding.a io         reflect.a  time.a
errors.a   io.a       regexp     unicode
expvar.a   log        regexp.a   unicode.a
flag.a     log.a      runtime

If you prefer not to pre-build and install the standard library to the GOROOT, required libraries will be built while building user packages. But,the standard libraries builds are not preserved for future use at this stage and they will be rebuilt each time you rungo build.

go語(yǔ)言學(xué)習(xí)參考:

1.Go 系列教程(Golang tutorial series) go語(yǔ)言中文網(wǎng)

2. go入門(mén)指南博客

3. go語(yǔ)言學(xué)習(xí)博客

4.go基礎(chǔ)學(xué)習(xí) coder python修行路

5.beego項(xiàng)目https://beego.me

6.https://github.com/golang/go

7.How to write Go Codehttps://golang.google.cn/doc/code.html

8.https://golang.google.cn/提供go在線(xiàn)測(cè)試環(huán)境和文檔

9.https://golang.google.cn/doc/go相關(guān)文檔

10.https://golang.google.cn/pkg/go標(biāo)準(zhǔn)庫(kù)

11.https://godoc.org/ go實(shí)用庫(kù)搜索

12.https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/readme.htmlGo語(yǔ)言高級(jí)編程(Advanced Go Programming)

13.在 GitHub 上構(gòu)建一個(gè)看上去正規(guī)的 Golang 項(xiàng)目

14.Go 語(yǔ)言設(shè)計(jì)與實(shí)現(xiàn)

總結(jié)

以上是生活随笔為你收集整理的go语言简述的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。