Go language Learning notes before words section

Source: Internet
Author: User

Objective

1. Specification for access modifiers

The go language requires public-modified variables to start with a capital letter

Variables with private modifier start with lowercase letters

This allows you to directly exempt the public and private keywords

2, curly braces position specification

If expression{if expression

...... {    ......
}                                                                          }

This is the correct wording, and this is the wrong writing.

3, go language error handling mechanism advantages, there are error types

4, when using the go language to imitate inheritance, if the structure of the parent class A and the structure of the child Class B has the same name of the attribute V, when B calls A's method output v value, the printout will be the parent class Property value

For example, the following code:

Package Main

Import "FMT"

Type Dog struct {

Name string

}

Type Bdog struct {

Dog

Name string

}

Func (this *dog) Callmyname () {

Fmt. Printf ("My name is%q\n", This.getname ())

}

Func (this *dog) GetName () string {

   Return this.name

}

Func Main () {
B: = new (Bdog)

B.name = "This is a Bdog name"

B.dog.name = "This is a Dog name"

B.callmyname ()

}
5, Go Language interface and type can be converted to each other, but the type to implement all the methods of the interface

Type Bird struct {
...
}

Func (b *bird) Fly () {
Fly in a bird's way
}

Type IFly Interface {
Fly ()
}

Func Main () {
var Fly IFly = new (Bird)
Fly. Fly ()
}

If you want to modify the methods in the interface later, you can redefine an interface to use, which can reduce the code modification and reduce the coupling

Go language Learning notes before words section

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.