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

What is the structure of Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "what is the structure of Go language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the structure of Go language" can help you solve your doubts.

Go Hello World instance

The basic components of the Go language are as follows:

Package declaration

Introduction packet

Function

Variable

Statement & expression

Annotation

Next let's take a look at the simple code that outputs "Hello World!":

Example

Package main

Import "fmt"

Func main () {

/ * this is my first simple program * /

Fmt.Println ("Hello, World!")

}

Let's take a look at the various parts of the above program:

The first line of code package main defines the package name. You must indicate which package the file belongs to in the first uncommented line of the source file, such as package main. Package main represents a program that can be executed independently, and each Go application contains a package called main.

The next line, import "fmt", tells the Go compiler that the program needs to use the fmt package (functions, or other elements), which implements the function to format IO (input / output).

The next line, func main (), is the function that the program starts executing. The main function must be included in every executable program, and it is generally the first function to execute after startup (if there is an init () function, it will be executed first).

The next line / *. * / is a comment and will be ignored when the program is executed. Single-line comments are the most common form of comments, and you can use single-line comments that start with / / anywhere. Multiline comments, also known as block comments, all begin with / * and end with * /, and cannot be used nested. Multiline comments are commonly used for documentation of packages or code snippets that are commented into blocks.

The next line, fmt.Println (...) You can output the string to the console and automatically add newline characters\ n at the end.

You can get the same result using fmt.Print ("hello, world\ n").

The Print and Println functions also support the use of variables, such as fmt.Println (arr). If not specified, they output the variable arr to the console in the default print format.

When identifiers (including constants, variables, types, function names, structure fields, etc.) begin with an uppercase letter, such as Group1, then objects using this form of identifier can be used by the code of the external package (the client program needs to import the package first), which is called an export (such as public in an object-oriented language) Identifiers that begin with lowercase letters are not visible to the outside of the package, but they are visible and available inside the entire package (like protected in object-oriented languages).

After reading this, the article "what is the structure of Go language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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