Go watch and practice-"Go Study Notes" series (iii)

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

1.7 Hands

Supports pointer types *T , pointer pointers **T , and contains package name prefixes * .T .

    • Default value nil , no NULL constant.
    • Operator "&" takes the variable address, "*" accesses the target object through the pointer.
    • Pointer operations are not supported, the "-and" operator is not supported, and the target member is accessed directly with ".".

You cannot add or subtract a pointer to an operation.

x := 1234p := &xp++ // Error: invalid operation: p += 1 (mismatched types *int and int)

You can unsafe.Pointer convert between and any type of pointer.

Returning a local variable pointer is safe, and the compiler allocates it on the GC Heap as needed.

func test() *int {    x := 100    return &x // 在堆上分配 x 内存。但在内联时,也可能直接分配在目标栈。}

will be Pointer converted into uintptr , can be implemented in disguise pointer operation.

Note: The GC uintptr is treated as a normal integer object, and it cannot prevent the "associated" object from being recycled.

1.8 Custom Types

You can divide types into named and unnamed categories. Named types are unnamed types, such as,,,, and so on, and, in bool int string array slice map relation to specific element types, lengths, and so on.

Unnamed types with the same declaration are considered to be of the same type.

    • A pointer with the same base type.
    • With the same element type and length array .
    • With the same element type slice .
    • With the same key-value type map .
    • With the same element type and direction of delivery channel .
    • Anonymous with the same field sequence (field name, type, label, order) struct .
    • The signatures are the same (parameters and return values, excluding parameter names) function .
    • The method set is the same (method name, method signature, and order independent) interface .

For example

var a struct { x int \`a\` }var b struct { x int \`ab\` }// cannot use a (type struct { x int "a" }) as type struct { x int "ab" } in assignmentb = a

typeYou can define a new type within a global or function.

func main() {    type bigint int64    var x bigint = 100    println(x)}

The new type is not an alias of the original type, except that they have the same data storage structure, there is no relationship between them, and no information of the original type is held. Unless the target type is an unnamed type, you must explicitly convert it.

x := 1234var b bigint = bigint(x) // 必须显式转换,除非是常量。var b2 int64 = int64(b)var s myslice = []int{1, 2, 3} // 未命名类型,隐式转换。var s2 []int = s

Next: Go to watch while practicing-"Go study Notes" series (iv)

    • This series is based on the rain marks of "Go Learning Notes" (fourth edition) collation compiled, thank you very much for the rain scar hard to pay and share!
    • Reprint Please specify: The article reprinted from: Hacker and Painter's Community [http://symphony.b3log.org]
    • If you think this chapter is doing well, please enjoy it below!

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.