Golang language Features

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

1. Preface

In the process of reading Docker and distribution source encountered a lot of pits, here to record these pits, update at any time, in the second section will give the updated content

2. Golang features included

    1. Godeps
    2. Vendor
    3. _Underline
    4. Interface Query
    5. Package naming
    6. Structs and Interfaces

2.Godeps

    1. After the golang1.1 version, with the GODEP Dependency management tool, the project under the new Godeps folder, which contains the project depends on the package, the record package depends on the file for Godeps.json, here is an example of distribution2.1 project Godeps, on GitHub . Com/docker/distribution can be directly used in the structure of the Github.com/spf13/cobra without importing, the ImportPath following package can be directly called Deps under the package containing:
{"Importpath":"Github.com/docker/distribution","Goversion":"go1.4.2","Packages": ["./..."],"Deps": [        {"Importpath":"Github.com/adroll/goamz/aws","Rev":"AA6E716D710A0C7941CB2075CFBB9661F16D21F1"},        {"Importpath":"Github.com/adroll/goamz/cloudfront","Rev":"AA6E716D710A0C7941CB2075CFBB9661F16D21F1"},        {"Importpath":"GITHUB.COM/ADROLL/GOAMZ/S3","Rev":"AA6E716D710A0C7941CB2075CFBB9661F16D21F1"},        {"Importpath":"Github.com/azure/azure-sdk-for-go/storage","Rev":"97D9593768BBBBD316F9C055DFC5F780933CD7FC"},        {"Importpath":"Github.com/sirupsen/logrus","Comment":"v0.7.3","Rev":"55eb11d21d2a31a3cc93838241d04800f52e823d"},        {"Importpath":"Github.com/bugsnag/bugsnag-go","Comment":"v1.0.2-5-gb1d1530","Rev":"b1d153021fcd90ca3f080db36bec96dc690fb274"},        {"Importpath":"Github.com/bugsnag/osext","Rev":"0dd3f918b21bec95ace9dc86c7e70266cfc5c702"},        {"Importpath":"Github.com/bugsnag/panicwrap","Rev":"E5f9854865b9778a45169fc249e99e338d4d6f27"},        {"Importpath":"Github.com/denverdino/aliyungo/oss","Rev":"0e0f322d0a54b994dea9d32541050d177edf6aa3"},        {"Importpath":"Github.com/denverdino/aliyungo/util","Rev":"0e0f322d0a54b994dea9d32541050d177edf6aa3"},        {"Importpath":"Github.com/docker/docker/pkg/tarsum","Comment":"v1.4.1-3932-gb63ec6e","Rev":"b63ec6e4b1f6f5c77a6a74a52fcea9564538c575"},        {"Importpath":"Github.com/docker/libtrust","Rev":"Fa567046d9b14f6aa788882a950d69651d230b21"},        {"Importpath":"Github.com/garyburd/redigo/internal","Rev":"535138d7bcd717d6531c701ef5933d98b1866257"},        {"Importpath":"Github.com/garyburd/redigo/redis","Rev":"535138d7bcd717d6531c701ef5933d98b1866257"},        {"Importpath":"Github.com/gorilla/context","Rev":"14f550f51af52180c2eefed15e5fd18d63c0a64a"},        {"Importpath":"Github.com/gorilla/handlers","Rev":"60c7bfde3e33c201519a200a4507a158cc03a17b"},        {"Importpath":"Github.com/gorilla/mux","Rev":"E444E69CBD2E2E3E0749A2F3C717CEC491552BBF"},        {"Importpath":"Github.com/inconshreveable/mousetrap","Rev":"76626ae9c91c4f2a10f34cad8ce83ea42c93bb75"},        {"Importpath":"Github.com/mitchellh/mapstructure","Rev":"482a9fd5fa83e8c4e7817413b80f3eb8feec03ef"},        {"Importpath":"Github.com/ncw/swift","Rev":"CA8CBBDE50D4E12DD8AD70B1BD66589AE98EFC5C"},        {"Importpath":"Github.com/noahdesu/go-ceph/rados","Comment":"v.0.3.0-29-gb15639c","Rev":"B15639C44C05368348355229070361395D9152EE"},        {"Importpath":"Github.com/spf13/cobra","Rev":"312092086bed4968099259622145a0c9ae280064"},        {"Importpath":"Github.com/spf13/pflag","Rev":"5644820622454e71517561946e3d94b9f9db6842"},        {"Importpath":"Github.com/stevvooe/resumable","Rev":"51AD44105773CAFCBE91927F70AC68E1BF78F8B4"},        {"Importpath":"Github.com/yvasiyarov/go-metrics","Rev":"57bccd1ccd43f94bb17fdd8bf3007059b802f85e"},        {"Importpath":"Github.com/yvasiyarov/gorelic","Comment":"v0.0.6-8-ga9bba5b","Rev":"a9bba5b9ab508a086f9a12b8c51fab68478e2128"},        {"Importpath":"Github.com/yvasiyarov/newrelic_platform_go","Rev":"B21fdbd4370f3717f3bbd2bf41c223bc273068e6"},        {"Importpath":"Golang.org/x/crypto/bcrypt","Rev":"c10c31b5e94b6f7a0283272dc2bb27163dcea24b"},        {"Importpath":"Golang.org/x/crypto/blowfish","Rev":"c10c31b5e94b6f7a0283272dc2bb27163dcea24b"},        {"Importpath":"Golang.org/x/net/context","Rev":"1dfe7915deaf3f80b962c163b918868d8a6d8974"},        {"Importpath":"Gopkg.in/check.v1","Rev":"64131543e7896d5bcc6bd5a76287eb75ea96c673"},        {"Importpath":"Gopkg.in/yaml.v2","Rev":"bef53efd0c76e49e6de55ead051f886bea7e9420"}    ]}

3.vendor

    1. golang1.5 version after adding Vendor Dependency package management tool, still in the trial phase, you need to import environment variables export go15vendorexperiment= 1, so you can directly in the writing go code when the package in the vendor directory without starting from Gopath, such as docker1.9 project, import "Github.com/docker/distribution/digest" The import is the corresponding package in the vendor directory

4. _ Underline

    1. _The use of underscores in Golang
      • One case is a placeholder, the method returns two results, and you only want one result, and the other uses _ placeholder, and if the variable is not used, the compiler will error.
      • There is another case similar to import _ "FMT" This, import a package come in, but do not use, just let the package itself initialized, run the package inside the init () function, or the import package will not be error. Remember, however, that it is not necessary to call the other functions or variables within the package in this case.

5. Enquiry

5.1 Interface Query

package twotype IStream insterface {    Read(buf []byteint, err error)    Write(buf []byteint, err error)}
package mainvarnew(File)if file5, ok := file1.(two.IStream); ok {}

The code above checks to see if the object pointed to by the File1 interface implements both. The IStream interface, if implemented, initializes the FILE5 and executes the specific code.

5.2 Types of queries

In the go language, it is also more straightforward to ask the object instance type that the interface points to, for example:

varinterface{} = ...switch v := v1.(type) {    caseint:     //现在v的类型是int    casestring:  //现在v的类型是string    ...}

6. Package naming

There can be only one package name in the same directory of the project, all files must use this package name, otherwise it will be an error.

7. Structs and Interfaces

    • When a struct member variable is a struct, if the struct is not assigned to a specific variable, then the value is deferred to the new struct, and if a new variable is assigned, the new variable is the member of the new struct.
    • When a struct implements all the functions of an interface, the struct implements this interface, not necessarily explicitly declaring
    • The interface type as a specific struct member, when assigning a value to the interface type of the struct, is the structure that has implemented the interface type

8. Introduction of the author

Liangmingyuan , the National Key Laboratory for parallel and distributed Computing (PDL) at Defense Hkust, started working with Docker at the beginning of 14, ready to contribute their code to the Docker-related open source community at the rest of the study period and prepare to continue the research after graduation. E-mail: liangmingyuanneo@gmail.com

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.