Golang underline (underscore) summary

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

I. Overview

"_" can be simply understood as the assignment but no longer used, in the Golang used more, the use of a lot of occasions, a little summary;

Second, the scene

1. Import

1 Import  "net/http/pprof"

The introduction of the package invokes the initialization function in the package, which only initializes the imported package and does not apply to other features in the package;

2, used in the return value

1  for _, V: = range Slice {}2 _, Err: = Func ()

Indicates that a value is ignored. A single function has multiple return values that are used to obtain a specific value

3. Use in variable

1 Interface {23} 4 5 struct {67} 8 9 var _ Interface = &t{}

The above is used to determine whether type T implements I, as a type assertion, and if T does not implement the excuse I, then the compilation error.

Example:

1 Package Main2 3Import"FMT"4 5Type InterfaceInterface {6 Stop ()7 }8 9Type BMWstruct {TenNamestring One } A  -Func ( This*BMW) Stop () { -Fmt. Printf ("%s BMW stop.\n", This. Name) the } -  -Type Carstruct { - Interface + } -  + Func Main () { Ac: =car{ at&bmw{ -Name:"X5", -         }, -     } - c.stop () -}

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.