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

Introduction to the basic grammar of golang

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Basic syntax of go language

This article uses easy-to-understand examples to introduce the basic grammar of golang. The content of the article is compact step by step. I hope you can get something according to this article. The following is the introduction and usage of the basic syntax:

Define variable

There are two ways to define variables in the go language: var, as follows

Var a = 1

Assign the a variable to 1 in the second way ": =", as follows:

A: = 1

As above, assign a variable to 1`Note: variable name: = variable value, this method is only applicable to the first assignment of a variable, if the variable has been assigned, such as the an above has been assigned to 1, at this time, if you want to re-assign a value in the use of a: = 2, you can change the assignment directly a = 2

Define the function:

There is no class keyword or class concept in the go language. Go is mainly a function-oriented and interface-oriented programming language.

Define the keyword func of the function, as follows:

Syntax: the content in [] is optional

Func function name ([parameter name] [parameter type]) [function return value type] {function specific logic [return] [returned content]}

Loop:

In go, there are only for loops and no other loops.

The basic way of writing:

For I: = 0; I = 10; iTunes + {loop body content}

4There is nothing wrong with it. Judge

1.if judgment:

If condition {processing logic} else {processing logic} switch and case judgment

Syntax: switch [variable name] {case condition 1: processing logic case condition 2: processing logic default: processing logic} the above method is a bit similar to case in shell

5. Slice slice

Define slice:

Variable name: = [length] type, as follows: define a slice of int type with length 10

Arr: = [10] int

Define a variable length slice:

Var arr1 = [...] Int {} or: arr: = [...] int {0Pert 1je 2jue 3jre 4jre 5jr 6jr 7}

Map type:

B: = make (map [string] string) c: = make (map [string] int) above is the definition of two map, namely, BMagi c make opens up memory space, map keyword, [] contains key type, string and int are value type so far, we have a basic understanding of the basic syntax of golang, but we still need to consolidate and practice for the specific usage, if you want to know more related content. Please pay attention to the industry information.

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

Servers

Wechat

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

12
Report