The "unspoken rules" in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
    • The way to provide outside access in the go language or the structure must be capitalized in the first letter

A struct is defined in a common header file, as follows:

Type Header struct {    ver       int    magic_num int    len       int}

In another execution file, initialization and assignment are made:

        VAR head comm. Header;        Head.ver = 1        head.magic_num = 0xffee        head.len = 0

Compile times wrong

./example_struct2binary.go:21:head.ver undefined (cannot refer to unexported field or method ver)

./example_struct2binary.go:22:head.magic_num undefined (cannot refer to unexported field or method Magic_num)

./example_struct2binary.go:23:head.len undefined (cannot refer to unexported field or method Len)

Later, it was recalled that the way to provide outside access in the go language or structure must be capitalized. This struct is capitalized only on the struct name, and the field in it is not capitalized, and the go language should be considered to be two different processes when the template is called, so the value cannot be found. The first letter of the field in the structure is changed to uppercase and then resolved.


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.