Go structure (similar to class in Python)

Time:2020-4-3

1、 Structure name attribute

package main
import "fmt"
func main(){
    type Sb struct {
        Name string // property name, data type
        Age int // if two attribute data types are the same, name, age int can be used
    }
    //He has two properties, not both of which need to be assigned
    //Position parameter {"you", 11}
    //But positional parameters must be passed in all attributes
    SB1: = sb {Name: "you", age: 11} // property name: property value
    
     
    fmt.Println(sb1)  //{you 11}
    fmt.Println(sb1.name) //you
}

2、 Structure anonymous property

package main
import "fmt"
func main(){
    type Sb struct {
         String // data type
         int
    }
    sb1 :=Sb{string:"you"}
    fmt.Println(sb1)  //{you 0}
    fmt.Println(sb1.string) //you
}

3、 Structure method

package main
import "fmt"

type Sb struct {
}

//Note that the point is not defined in the main function
Func (sb sb) speak() {// func (struct object struct type) method name() {}
    FMT. Println ("mm")
}


func main(){
    wq :=Sb{}
    wq.Speak()
}

4、 Nested structure

type Hobby struct {
    name string
    id int
}
type Person struct {
    name  string
    Hobby // structure name
}
func main(){
    a := Person{name: "p1",Hobby:Hobby{name:"LOL",id:1}}
    
    FMT. Println (a.hobby. Name) // if the name has a duplicate name, you need. Structure. Attribute
    
    FMT. Println (a.id) // if there is no duplicate property name, it will be promoted
    //FMT. Println (a.hobby. ID) you can also get it in this way
}

/*
Personal understanding can be understood as the inheritance of classes in Python, such as a inherits B
type B struct {int}
type A struct {B}
But there are also differences. The names in the two classes can coexist, but not in Python
*/

5、 Structure is the parameter of method and the property of structure is modified

package main
import "fmt"

type Person struct {
    name  string
}

Func changename (p * person, newname string) {// if the original class is not changed, only values can be passed through the structure object
    (* p.name) = newname // can also be written as p.name = newname
}
func main(){
    a := Person{name: "p1"}
    ChangeName(&a,"ywy")
    fmt.Println(a.name)
}

6、 Structure is the parameter of method and does not change the property of structure

package main
import "fmt"

type Person struct {
    name  string
}

func ChangeName(p Person,NewName string){   
    p.name = NewName  
}
func main(){
    a := Person{name: "p1"}
    ChangeName(a,"ywy")
    fmt.Println(a.name)
}

7、 Struct type assertion

Write a way:

package main
import "fmt"

type Person struct {
    name  string
}
func Test(a interface{}){
    _,err :=a.(*Person)
    if !err{
        FMT. Println ("is person")
    }
}

func main(){
    a := Person{name: "p1"}
    Test(a)
}

Writing two:

package main

import (
    "fmt"
)

type Person struct {
    name string
}

func Test(a interface{}) {
    Switch A. (type) {// if you want to get the object of a, assume: = A. (type)
    case Person:
        FMT. Println ("is person")
    default:
        FMT. Println ("not person")
    }
}
func main() {
    a := Person{name: "p1"}
    Test(a)
}

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]