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

How to use Go language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how to use the Go language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

Use Go Playground

Step 1: open https://play.golang.org and enter the Hello World program in the edit box:

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

The following figure shows: step 2: click the "Run" button, you can see the output of the program "Hello,world". Congratulations, you have written your first Go program and correctly output the results.

Go language features

Although the Hello World program is simple, it still contains some core concepts of the Go language. Next, let's interpret the Hello World program one by one.

First line: package main

The first line of the program declares a package named main. The code of the Go language is organized by package, and the concept of package is similar to the concept of package,module in other languages, which is a logical collection of codes with the same functions. A package may contain one or more .go source code files. Each source file starts with package. For example, in our case, it is package main. This line of declaration statement indicates which package the file belongs to.

It should be noted that package main is a special package. Main package is the entrance to the Go program. To be exact, the entry to the Go program is the main method in the package named main (that is, the main method in the example).

Second line: import

The package declaration statement is followed by the import statement. The import statement introduces other package into the current file, so that variables, constants, types, methods, etc., from other package can be used in the current file. Go's import is similar to Java's import,C++ 's include. The Go standard library has provided more than 100 package,fmt this package contains various functions that accept input and format output. Println is one of the common functions that can output a piece of text in a format.

Line 3: func main

The third line declares a function named main. Use the func keyword in the go language to declare a function. The format is:

Func function name (parameter name 1 parameter type 1, parameter name 2 parameter type 2,...) returns type 1, return type 2,.

For example, the following methods:

Func sayHello (name string, age int) string

A sayHello method is declared. Receive two parameters: name of type string and age of type int. And returns a value of type string. As mentioned earlier, in the package of main, the main function is also a special function, which is the entrance to the whole program (in fact, almost all C languages are like this).

Line 4: fmt.Println

The fourth line is the logic that the program actually executes, which outputs "Hello world". Fmt is a package introduced through import and provided by the go standard library. Println is a method defined in the fmt package to output text to standard output.

Features of Go language

Through Hello world, we can find some features of the Go language:

The semicolon is not required at the end of the statement. This is different from C language, Java and other languages.

The Go language does not need to show the scope of declaring methods or variables. That is, there is no need to explicitly declare public or private. In fact, the Go language determines the scope by whether the first letter of a variable or method name is uppercase or lowercase. The uppercase letter begins with public and the lowercase letter begins with private.

There are also some characteristics of Go, which are not reflected because the Hello world program is relatively simple. Let me also say this by the way:

When the Go language declares variables and method parameters, the name comes first and the type comes after. Like var name string. This is different from C language and Java language.

The Go language has taken a very tough attitude towards the code format. The gofmt tool provided by go will format your code into a standard format (this formatting tool does not have any parameters to adjust the code format, the Go language is so wayward), stipulating a standard code format can avoid endless meaningless jihad (tearing) (also led to the Go language's TIOBE ranking is low, because of the lack of torturing topics). This philosophy of Go ensures that collaboration between teams is more standardized and efficient when a large team develops and maintains a project together.

Thank you for reading this article carefully. I hope the article "how to use Go language" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Internet Technology

Wechat

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

12
Report