Get the App
SLTechnology News&Howtos  ›  Development  › 

How Go language constants are used as enumerations

Shulou Source: shulou.com Published: 2022-06-01 02:34:13 10月30日 Update

This article introduces the relevant knowledge of "how Go language constants are used as enumerations". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Constants can also be used as enumerations:

const ( Unknown = 0 Female = 1 Male = 2 )

The numbers 0, 1 and 2 represent unknown gender, female and male respectively.

Constants can be evaluated using len(), cap(), unsafe.Sizeof() functions. In a constant expression, the function must be a built-in function, otherwise it compiles but:

examples

package main

import "unsafe"

const (

a = "abc"

b = len(a)

c = unsafe.Sizeof(a)

)

func main(){

println(a, b, c)

}

The results of the above examples are:

abc 3 16"How to use Go language constants as enumerations" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Constants functions language content examples more knowledge expressions practicality achievement next representatives dilemmas women reality gender situations numbers articles cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Huawei OPPO Reno Linux