Go Language Bible 1.3-assignment and type declaration

Source: Internet
Author: User
Tags arithmetic operators gcd greatest common divisor

This is an article created on, where the information may have evolved or changed.
Assignment

The value of the variable can be updated by the assignment operator =, v = 10.

x = 1 // named variable x
* p = true // pointer variable
person.name = "bob" // struct field
count [x] = count [x] * scale // an element of an array, slice, or map


Both arithmetic operators and bit operators have corresponding unary operator forms, v = v + x is equivalent to v + = x, for example:
count [x] * = scale
This abbreviated form can save a lot of repetitive work, and numeric variables can also be incremented or decremented by ++:
v: = 1
v ++ // same as v = v + 1; v becomes 2
v-- // same as v = v-1; v becomes 1 again

1. Tuple assignment Tuple assignment allows assignment of multiple variables at once. = The expression on the right will be evaluated before the variable on the left is updated. When = the same variable is on both the left and right sides, this method of evaluation is very useful, for example, to exchange the values of two variables:
x, y = y, x

a [i], a [j] = a [j], a [i]
Another example is to calculate the greatest common divisor (GCD) of two numbers:
func gcd (x, y int) int {
    for y! = 0 {
        x, y = y, x% y
    }
    return x
}
Or calculate the Nth value of the Fibonacci sequence:
func fib (n int) int {
    x, y: = 0, 1
    for i: = 0; i <n; i ++ {
        x, y = y, x + y
    }
    return x
}
The
Tuple assignment can also combine some scattered assignments:
i, j, k = 2, 3, 5
  However, if the expression is more complex, tuple assignment should be avoided as much as possible, and the readability of separate assignments will be better.

    Some specific expressions, such as function calls have multiple return values, in this case = there must be a corresponding number of variables on the left side to assign:

f, err = os.Open ("foo.txt") // The function call has two return values
The
The

Many times, the function will use this multiple return value feature to return an additional value, this value will indicate whether the function call is successful (may be an error type variable err, or bool type variable ok). Finding a value in the map, type assertion, receiving value from the channel, etc. will have two return values, of which the second value is a bool type:

v, ok = m [key] // map lookup
v, ok = x. (T) // type assertion
v, ok = <-ch // channel receive
The
The

Go language also supports anonymous variables (readers who have used functional languages should understand this mechanism), we can assign undesired values to blank identifiers (= the number of variables on the left and the number of values on the right must be the same):

_, err = io.Copy (dst, src) // Only care about the success of Copy, don't care about the number of specific Copy bytes, so discard the first value
_, ok = x. (T) // Only care about the success of the type assertion, not the specific value of x, so discard the first value

   2. Assignability

The above assignment statement is an explicit assignment, but in some cases, implicit assignment will occur: in the function call, implicit assignment to the function parameter; when the function returns, implicit assignment to the return operand; and Combination types similar to the following:

medals: = [] string {"gold", "silver", "bronze"}
Here is the implicit assignment, the equivalent explicit form is this:
medals [0] = "gold"
medals [1] = "silver"
medals [2] = "bronze"
The same is true for map and channel types, which all support this implicit assignment.

Whether it is explicit assignment or implicit assignment, as long as = the left and right sides have the same type.

The

The

Specific assignment rules for different types will be explained in detail in subsequent chapters. For the types we have discussed so far, the rule is simple: the types must match exactly (hence Go is a strongly typed static language), and nil can be assigned to an interface or other reference type. Constant assignment is very flexible during type conversion and can avoid most explicit type conversions:

const x = 112
var v float64 = x
fmt.Println (v) // output: 112
Whether two variables can be compared with == or! = Depends on the assignability, a == b can only be judged when a = b is feasible.

The

Type declaration

The type of variable defines some personalized attributes of the variable, such as the size of the memory occupied by the variable, the arrangement in the memory, the internal form of the variable, the operations supported by the variable, and the behavior of the variable.

In the actual project, many custom types have the same underlying type, for example: int can be a circular index, timestamp, file descriptor or a month; float64 type can be vehicle speed, temperature. Use type to declare named types, so you can build your own timestamps, file descriptors, and other types on top of the underlying types.

type name underlying-type
Named type declarations generally occur at the package level, so the type is visible within the package. If the type is exported (capitalized first), then it is globally visible. In order to explain the type declaration, here different temperature measurement units are set to different types:
package tempconv

import "fmt"

type Celsius float64
type Fahrenheit float64

const (
    AbsoluteZeroC Celsius = -273.15
    FreezingC Celsius = 0
    BoilingC Celsius = 100
)

func CToF (c Celsius) Fahrenheit {return Fahrenheit (c * 9/5 + 32)}

func FToC (f Fahrenheit) Celsius {return Celsius ((f-32) * 5/9)}
Two types of Celsius (degrees Celsius) and Fahrenheit (degrees Fahrenheit) are defined above as two different units of temperature. Even if the underlying types of the two types are the same float64, they are completely different types, so they cannot be compared with each other, nor can they be combined in arithmetic expressions. This rule can avoid the wrong use of two different Temperature unit. Because the types of the two temperature units are different, the two values returned by CToF and FToC are also completely different.




For each type T, there is a corresponding type conversion T (x), you can convert x to T type. Type conversion can only be performed if and only if the two types have the same underlying type, such as Celsius and Fahrenheit above, or both types are pointer types, pointing to the same underlying type.

Conversions can be performed between numeric types, between string and [] byte, and these conversions may change the representation of the value. For example, converting a floating-point number to an integer will intercept the small tree part; converting a string to [] byte slice will allocate memory space to create a copy of the string (memory copy is often one of the performance bottlenecks). In short, the type conversion is completed at compile time, and it will not fail at run time!

The

The

The underlying type of a named type determines its structure and manifestation, as well as the basic operations it supports (which can be understood as inheriting from the underlying type), just like using the underlying type directly. So for Celsius and Fahrenheit, arithmetic operations supported by float64, they both support:

fmt.Printf ("% g \ n", BoilingC-FreezingC) // "100" ° C
boilingF: = CToF (BoilingC)
fmt.Printf ("% g \ n", boilingF-CToF (FreezingC)) // "180" ° F
fmt.Printf ("% g \ n", boilingF-FreezingC) // compile error: type mismatch
Another example:
type temp int
func main () {
var x1 temp = 1
var x2 temp = 2
fmt.Println (x1 + x2) // output: 3

}

If two values have the same named type, you can use the comparison operators == and <to compare; or two values, one is a named type, one is the underlying type of the named type, you can also compare. But two different named types cannot be directly compared:

var c Celsius
var f Fahrenheit
fmt.Println (c == 0) // "true"
fmt.Println (f> = 0) // "true"
fmt.Println (c == f) // compile error: type mismatch
fmt.Println (c == Celsius (f)) // "true"!
We all know that floating-point numbers are imprecise expressions, so comparisons between two floating-point numbers require extra care. Note here the comparison between floating point numbers after the last type conversion. Celsius (f) does not change the value of f, because both c and f are initialized to 0, so you can safely compare.



If you need to repeatedly write a complex type in some parts of the project, using named variables can bring great convenience.



We can also define unique behaviors for named types. These behaviors are the type methods in Go, which we will explain in detail in subsequent chapters.

The following code defines a method of Celsus type: String,


func (c Celsius) String () string {return fmt.Sprintf ("% g ° C", c)}

Many custom types will define a String method, because when calling the function of the fmt package to print the type, String will control the printing method of the type (in fact, the Stringer interface is implemented, unlike other OO languages, Go language The interface implementation is implicit):
c: = FToC (212.0)
fmt.Println (c.String ()) // "100 ° C"
fmt.Printf ("% v \ n", c) // "100 ° C"; no need to call String explicitly
fmt.Printf ("% s \ n", c) // "100 ° C"
fmt.Println (c) // "100 ° C"
fmt.Printf ("% g \ n", c) // "100"; does not call String
fmt.Println (float64 (c)) // "100"; does not call String




Everyone is welcome to join the QQ group QQ group 894864, which has a lot of enthusiasm!

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.