This is a creation in Article, where the information may have evolved or changed.
One project Main.gopackage mainimport "FMT"//for int type integertype integer intfunc (a integer) more (b integer) integer {return a-b}func Modify_arr (arr [3]int) {///array is a value type, the pass-through function is a copy of the arr[0] = 500}func modify_slice (arr []int) {///slice is a reference type, the input letter The number inside can be directly modified arr[0] = 500}//defines the rect struct body type rect struct {x, y float64width, Height float64}//The method of increasing the calculated area for the RECT structure func (R * Rect) area () float64 {return r.width * r.height}//creates a function that initializes a Rect func newrect (x, y, width, height float64) *rect {return & ; Rect{x, y, width, height}}//declaration basetype base struct {Name string}//adds Funca and Funcbfunc (base *base) Funca () {FMT) to base. Println ("Funca")}func (base *base) FUNCB () {fmt. Println ("FUNCB")}//inherit base childrentype children struct {Baseson string}//Add Children method func (FUNCC children) for *children FUNCC () {fmt. Println ("FUNCC")}//define Flie struct and implement four methods type File Struct{}func (f *file) Read (buf []byte) (n int, err error) {return n, err}fun C (f *file) Write (buf []byte) (n int, err error) {return n, Err}func (f *file) Seek (off int64, WHence int) (POS int64, err Error) {return pos, Err}func (f *file) Close () error {return nil}//interface type IFile interface {Read ( BUF []byte] (n int, err error) Write (buf []byte) (n int, err error) Seek (off int64, whence int) (POS int64, err Error) Close ( ) Error}type Ireader Interface {Read (buf []byte) (n int, err error)}type Iwriter interface {Write (buf []byte) (n int, err Error)}type Iclose interface {Close () error}//Interface Assignment func (a integer) less (b integer) bool {return a < B}func (a *integer) Add (b integer) {*a + = b}//Define Interface Lessaddertype Lessadder interface {less (b integer) booladd (b integer)}func main () {//intege R has all of the int types, and the less and more methods of the int type do not have the var v1 Integer = 1//call Interger, if v1. Less (+) {FMT. Println (v1, "less 50")}//Test Array v2: = [3]int{1, 2, 3}modify_arr (v2) fmt. Println (v2)//[1 2 3]//Test slice v3: = []int{1, 2, 3}modify_slice (v3) fmt. Println (v3)//[500 2 3]//test What type of struct the new comes out of testRect1: = new (Rect) testrect1.x = 500testrect2: = testrect1testrect2.x = 300FM T.println (TestRect1, TestRect2)//&{0 0 0} &{300 0 0 0}//creates an initialization Rect type rect1: = new (Rect) fmt. Println (rect1) Rect2: = &rect{}fmt. Println (rect2) Rect3: = &rect{0, 0, 200}fmt. Println (rect3) Rect4: = &rect{width:200, height:400}fmt. Println (RECT4) Rect5: = Newrect (a) fmt. Println (RECT5)//basebase1: = &base{"Yunxuan"}base1.funca () BASE1.FUNCB () Children1: = &children{*base1, " Peixuan "}children1.funca () CHILDREN1.FUNCC ()/* Implement an interface inside the method is considered to implement this interface File implementation of these interfaces */var file1 IFile = new (File) file1. Close () var file2 ireader = new (File) file2. Read ([]byte{}) var file3 iwriter = new (File) file3. Write ([]byte{}) var v4 Integer = 1var V5 lessadder = &v4v4. ADD (v4) V5. ADD (v4)}
Notes from Http://blog.csdn.net/zhouyunxuan