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

The usage of const constant in Go language

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

Share

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

This article introduces the knowledge of "the use of const constant in Go language". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Const constant in Go language

The constant keyword of the Go language is const, which is used to store values that will not change. The format defined is:

Const valueName type = value or const valueName = value

Const a = "abc" const b string = "Hello"

The constant in Go has the basic characteristics of constant and cannot be modified. The reason is that the value of the constant is determined at compile time and cannot be modified at run time. Examples are as follows:

Package main

Import ("fmt")

Func main () {const a = "abc" const b string = "Hello" a = "Word" fmt.Println ("Hello, playground:", output:./prog.go:10:4 b)} output:./prog.go:10:4: cannot assign to a

In addition, constants in the Go language can also compile errors when trying to print an address because there is no data type * const in the Go language.

Package main

Import ("fmt")

Func main () {const a = "abc" const b string = "Hello" fmt.Println ("Hello, playground:", & const b)} output:./prog.go:11:35: cannot take the address of a

Constant definitions in the Go language have some default rules, mainly:

1. There must be a value when defining a constant.

two。 If a constant is not assigned to a constant in a constant group, the value of the constant is the same as the previous one.

3. If a constant is mixed with iota, the iota will count from 0, and one more constant will increase by 1.

Examples are as follows:

Package main

Import ("fmt")

Func main () {const (a = 2b / / bao2c = 5d / / dong5e / / eBay 5) fmt.Println ("Hello, playground:", a, b, c, d, e)

There are two constants in front of const (f = 1 g = 1 h = iota / /), so here h = 2 I / / I will automatically + 1 j / / after h, and automatically + 1 k = 10 l = iota / / iota will not change because of the above k value. But the change is still 6) fmt.Println ("Hello, playground:", f, g, h, I, j, k, l)} output: Hello, playground: 22 55 5Hello, playground: 1 1 2 3 4 10 6

II. Const constant in C++

The const constant in C++ means that the value of the data cannot be changed. It has the following characteristics:

1. The specific implementation is that in the compilation phase, the compiler puts the const-modified constant into the symbol table, and when the constant is used at run time, the value can be directly extracted from the symbol table.

The constant modified by 2.const still has an address, and the value in that address can also be changed (Note: this refers to the const constant in the local variable).

3.const-decorated global variables and static variables are stored in the global read-only space, and the address at this time cannot be changed at run time, because if read-only memory is modified, it will be directly crash.

Examples are as follows:

# include using namespace std

Int main () {const int a = 10; int* p = (int*) (& a); * p = 20; cout

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