Go-Variables and constants

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Defining variables

There are many ways to define variables in Golang

Declared before use, if the defined variables are not used compile-time error
A.

/* Define a single variable */var varName type          //defines a type of variable varnamevarname = value//           assigns variable varName var varName type = value  / /define a variable and assign an initial value/* Define multiple variables */var varName1, varName2 type              //Define two variables of type types VarName1 and varname2varname1, varName2 = Val1, val2
  //assigns a value to two variables, varName1 = Val1 ... var varName1, varName2 type = Val1, Val2//define two type variables, varName1 = Val1 ...
B.
/* Single */var varName = Val                   //define variables and assign initial values, the system defines the type of varName (variable) based on the Val (value)/* Multiple */var varName1, varName2 = val1, Val2//var Name1 = Val1 ... Ibid.
C.
/* Single */varname: = Val                   //Same as B, depending on the value to determine the variable type/* multiple */varname1, varName2: = Val1, Val2//varname1 = val1 ...//with ": =" instead of Var t Ype, the wording is more concise, but this declaration method can only be used inside the function, or the compilation will be error
D.
_, VarName2: = Val1, val2  //Discard val1, varName2 = Val2//_ (underscore) is a special variable, the value assigned to it is discarded

E.

Use () to declare when multiple variables are defined, reducing the amount of code

var (    varName1 type    varName2 type)//define two variable var (    varName1 = val1    varName2 = val2)//Define two variables and assign a value

2. Constants

Constants: Values that cannot be changed in a program are generally defined as numeric values, Boolean values, strings, etc.

Format:

Const Constname [Type] = val//defines a constant Constname = Val, type can be omitted, and the system will determine the type according to Val at compile time

Note:

1). var num = 3//Actually 3 is also called a constant

2). An expression in which Val can be an expression but cannot be used for runtime to know the result

3). Pre-defined constants: True/false/iota

4). When defining multiple constants, you can also use the following method

Const (    constName1 [Type] = Val1    constName2 [Type] = Val2)


Note_var_const.go code List

The package main//declares the current file's name, and Main is a standalone package that is compiled to generate an executable file import "FMT"//import package var id = 123456/*id2: = 654321//in external function: =, error at compile time Non-declaration statement outside function body*/const PI = 3.14//Each program that can be run independently contains the entry function main, same as other languages, but no parameters and return values func main () {var num intnum = 100fmt. PRINTLN (num)//Output 100var num1, num2 intnum1, num2 = 1, 2fmt. Println (NUM1, num2)//Output 1 2var no1, NO2 = 3, 4fmt. Println (No1, NO2)//Output 3 4n1, N2: = 5, 6fmt. Println (n1, N2)//Output 5 6_, N: = 7, 8fmt. PRINTLN (n)//Output 8var (key1 stringkey2 string) key1, Key2 = "K1", "K2" FMT. Println (Key1, Key2)//Output K1 k2var (a = 9b = ten) fmt. Println (A, B)//Output 9 10fmt. PRINTLN (ID)//output 123456fmt.  PRINTLN (PI)//Output 3.14/*PI = 3.1415//Change the value of the constant, compile error//cannot assign to pi//cannot use 3.1415 (type float64) as type ideal in assignment*/}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.