Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Features of Go language and how to use basic data types

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the characteristics of Go language and how to use basic data types". In daily operation, I believe that many people have doubts about the characteristics of Go language and how to use basic data types. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use GE language features and basic data types. Next, please follow the editor to study!

A brief introduction to Golang

Golang (also known as Go) is a static strongly typed, compiled, parallel programming language developed by Google, and has the function of garbage collection.

The characteristics of 1.Go language

Go language ensures not only the security and performance of static compiled language, but also the high efficiency of dynamic development language maintenance. An expression is used to describe Go language: Go = C + Python, which shows that Go language not only has the running speed of C static language program, but also can achieve the rapid development of Python dynamic language.

1) inherit a lot of ideas from C language, including expression syntax, control structure, basic data types, call parameter values, pointers, etc., and retain the same compilation and execution mode and weakened pointers as C language.

Func testPtr (num * int) {* num = 20}

2) the concept of package is introduced, which is used to organize the program structure. A file in Go language belongs to a package and cannot exist alone.

Package mainimport "fmt" func main () {fmt.Println ("Hello World")}

3) garbage collection mechanism, automatic memory collection, no need for developer management.

4) Natural concurrency: support concurrency from the language level, easy to implement; goroutine lightweight threads, can achieve large concurrency processing, efficient use of multi-core; based on CPS concurrency model.

5) absorb the pipeline communication mechanism to form the unique pipeline Channel of Go language. Through the pipeline Channel, we can realize the mutual communication between different goroute.

6) the function can return multiple values:

Func getSumAndSub (sum int,sub int) (int,int) {Sum: = sum + sub Sub: = sum-sub return Sum,Sub}

7) New innovations, such as Slice slicing, Defer delayed execution, etc.

Variable scope of 2.Golang

Local variable: a variable defined within a function whose scope is limited to the interior of the function (not even if the first letter is capitalized)

Global variable: a variable defined outside a function whose scope is valid throughout the package.

But when a variable is defined in a code block, such as in if for, then the scope of the variable is valid in that code block.

Another is: assignment statements can not be defined outside the function, such as: this name: = "zhangsan", we can change to var name string = "zhangsan".

Two ways for 3.Golang to execute the process

Compile first, after execution: go build-o Hello.exe test.go (you can run everywhere after compilation, but the file will be larger after compilation)

Run directly: go run test.go (run directly, which is relatively slower than the previous compilation, and depends on the Go environment)

Variables or import packages defined by the PS:Go language cannot be compiled and passed without the use of put.

Second, the basic operation of Golang 1. Install the Golang language development package on Linux

1) install the Golang package

[root@localhost ~] # wget https://golang.google.cn/dl/go1.17.3.linux-amd64.tar.gz[root@localhost ~] # tar xf go1.17.3.linux-amd64.tar.gz-C / usr/local/ [root@localhost ~] # ln-s / usr/local/go/bin/* / usr/bin/ [root@localhost ~] # go versiongo version go1.17.3 linux/amd64

2) configure the development environment of Golang

[root@localhost ~] # mkdir-p ~ / Go-Test/src # Golang source code directory [root@localhost ~] # mkdir-p ~ / Go-Test/pkg # Golang compiled library file [root@localhost ~] # mkdir-p ~ / Go-Test/bin # Golang compiled executable file [root@localhost ~] # cat ~ / .bashrcexport GOROOT=/usr/local/goexport GOPATH=~/Go-Testexport PATH=$PATH:$GOROOT/bin:$GOPATH/binEND [root@localhost ~] # source ~ / .bashrc [root@localhost ~] # go env # check whether the environment variable of Go modifies the directory in the file

3) write Go code for verification

[root@localhost ~] # cat

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report