The "1.10" version of go language supports xp system. Golang1.10 is the last version that supports Windows XP/2003, so if you want Golang-compiled programs to run on WinXP/2003, you need to download golang1.10; later versions require at least Win7/2008 operating system.
The operating environment of this tutorial: linux7.3 system, GO version 1.18, Dell G3 computer.
Go language development version
The development of Go language programs mainly relies on the golang compiler. Compilers are divided into GC (Go Compiler) and GCCGo. Currently, the official version of golang that can be downloaded is the GC version, while GCCGo is maintained by the GCC organization.
golang1.10 is the last version that supports windows xp/2003, so if you want golang-compiled programs to run on winxp/2003, you need to download golang1.10. Later versions require at least Win7/2008 operating system.
For the Linux operating system, the minimum kernel version supported by golang is 2.6.23. For Redhat, this is at least RHEL6.0 (2.6.32-71); for CentOS, it is at least CentOS6.0 (2.6.32-71); for Oracle Linux, it is at least OL5.6 (2.6 .32-100.26.2.el5uek); for Ubuntu, at least 10.04; for Suse Linux, at least SLE11 (Suse Linux Enterprise 11). [Related recommendations: Go video tutorial, Programming teaching]
For Unix, FreeBSD can use GC, but for operating systems with non-x86 processors , you need to use the GCCGo compiler.
Elements of Go language development
(1 ) Compiler
A command tool that compiles code into a binary executable file. In fact, it should be more appropriately called "builder" here. Because after the code is written, enter the "go build" command to directly "compile" and "link" the code into an executable file.
The definitions of the terms "compilation" and "link" come from the C language
C language code is organized by fixed vocabulary according to a fixed format. It is simple and intuitive, and is easy for programmers to identify and understand. But for the CPU, the C language code is a bible, which is not recognized at all. The CPU only recognizes a few hundred instructions in binary form. This requires a tool to convert C language code into binary instructions that the CPU can recognize. This tool is a special software called a compiler.
After the C language code is compiled, it does not generate the final executable file (.exe file), but generates an intermediate file (or temporary file) called an object file (Object File). The target file is also in binary form, which has the same format as the executable file. For Visual C, the object file suffix is .obj; for GCC, the object file suffix is .o.
The target file can become an executable file after being linked. Since the format of the target file and the executable file are the same, why do we need to link it again? Can't it be used directly as an executable file?
No way! Because compilation only converts the code we write into binary form, it also needs to be combined with system components (such as standard libraries, dynamic link libraries, etc.), which are necessary for program running.
Link is actually a "packaging" process, which combines all binary form target files and system components into an executable file. Completing the linking process also requires a special software called a linker.
(2) Extension package/component
In the process of program writing, if all "support programs" or "tool classes" are started by the developer Starting from scratch, the workload will become huge. Therefore, to improve development efficiency, in most cases you will try to find "extension packages" or "components" that assist program development. Golang comes with its own package management tool, so you can download expansion packages through commands according to specific needs during development.
(3) Code writing and debugging
You can develop code through any text editor, and then compile the program through the GC compiler, or of course through "go run" The command is run directly. If you need to debug a program, in most cases the safest, most secure, and most trustworthy way is to output logs in the program. If you must have a single point of debugging, you can use delve.
Environment setup
(1) Download and unzip
cd usr/local wget https://golang.google.cn/dl/go1.15.6.linux-amd64.tar.gz tar xf go1.15.6.linux-amd64.tar.gz cd go
(2) Create a soft connection
ln -s usr/local/go/bin/go usr/local/bin/go
(3) Example: Hello world
cd usr/local/src mkdir -p go/helloworld cd go/helloworld/ vim main.go
Enter the following code:
package main import( "fmt" ) func main(){ fmt.Println("Hello world") }
Save and exit, then run the following command
go run main.go
At this time, the system will have the following output:
Build
如果每次都通过go run去执行程序,则需要在发布程序时需要带上golang的运行时环境。这显然是不能满足要求的。可以将程序直接构建为可执行文件。
键入如下命令:
go build
此时,会在当前目录下产生一个helloworld的可执行文件
运行这个文件,会得到与go run 相同的结果。
./helloworld
使用扩展包
golang的扩展包有多种不同的管理方式。比如传统的 go get 方式以及新推出对的go module方式。
对于go get方式,由于某些特殊原因,部分官方包无法从golang.org下载,因此需要采取手动下载与go get相结合的方式。
对于go module方式,由于可以采用goproxy代理的方式获取golang.org的基础包,因此完美解决了国内golang开发的难题。
然而go module只有1.13版本之后的golang才能支持。而1.13是不支持winxp/2003的。并且对于go get与go module而言,有关代码在引包的时候,写法多少有些不同。因此如果要开发兼容低版本操作系统的程序,仍然建议采用go get进行包管理。
这里只介绍通过go module管理包的方式
以读取mongodb数据为例,介绍如何使用go module引入mongodb客户端扩展包,并读取数据。
设置goproxy代理
go env -w GOPROXY=https://goproxy.cn,direct
创建项目目录,并初始化
mkdir -p go/mongodemo cd go/mongodemo/ go mod init mongodemo
此时,会在mongodemo目录下生成go.mod文件
然后编辑main.go,输入如下代码:
package main import ( "fmt" "log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type cfg_host struct { Host string Cfg_data string } func main() { user := "用户名" password := "密码" port := "端口" ip := "localhost" url := fmt.Sprintf("mongodb://%s:%s@%s:%s",user,password,ip,port) session, err := mgo.Dial(url) if err != nil { panic(err) } defer session.Close() Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) c := session.DB("db").C("cfg_hosts") var cfgs []cfg_host err = c.Find(bson.M{}).All(&cfgs) if err != nil { log.Fatal(err) } for _,item := range cfgs { fmt.Printf("%s\n",item.Host) fmt.Printf("%s\n",item.Cfg_data) } }
构建
go build
此时,go module
会自动下载依赖包
同时,生成了名为“mongodemo”的可执行文件
执行该文件,输出了mongodb中的查询结果
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of Which version of go language supports xp. For more information, please follow other related articles on the PHP Chinese website!