How to use map, slice, struct and interface types of Golang functions

PHPz
Release: 2023-05-17 08:02:07
Original
1092 people have browsed it

Golang is an open source programming language developed by Google. It uses static typing and garbage collection mechanisms, and supports concurrent programming. Functions are an important component in Golang, and map, slice, struct and interface are commonly used data types in functions. This article will introduce the use of these data types in Golang.

1. Map type

map is a key-value pair data structure. In Golang, its use is very simple. The following is an example:

package main

import "fmt"

func main() {
    m := make(map[string]int)
    m["apple"] = 1
    m["banana"] = 2
    m["orange"] = 3
    fmt.Println(m)
}
Copy after login

In the above example, we first use the make function to create an empty map, and then add key-value pairs to the map in sequence. Finally, the map is output using the println function in the fmt package.

In addition to using the make function to create a map, we can also use literals to create a non-empty map, as shown below:

package main

import "fmt"

func main() {
    m := map[string]int{"apple": 1, "banana": 2, "orange": 3}
    fmt.Println(m)
}
Copy after login

We can also use map when declaring variables Declare them together as follows:

package main

import "fmt"

func main() {
    var m map[string]int
    m = make(map[string]int)
    m["apple"] = 1
    m["banana"] = 2
    m["orange"] = 3
    fmt.Println(m)
}
Copy after login

Of course, if a key-value pair does not exist in the map, we can use the delete function to delete it, as shown below:

package main

import "fmt"

func main() {
    m := make(map[string]int)
    m["apple"] = 1
    m["banana"] = 2
    m["orange"] = 3
    delete(m, "banana")
    fmt.Println(m)
}
Copy after login

2. slice Type

slice is a dynamic array where elements can be added or removed at any time. The following is an example:

package main

import "fmt"

func main() {
    s := make([]string, 3)
    s[0] = "apple"
    s[1] = "banana"
    s[2] = "orange"
    s = append(s, "grape")
    fmt.Println(s)
}
Copy after login

In the above example, we use the make function to create a slice with an element type of string and a length of 3. Then, we added 3 elements to the slice, and finally added a new element to the slice using the append function.

Of course, we can also use literals to create a non-empty slice, as shown below:

package main

import "fmt"

func main() {
    s := []string{"apple", "banana", "orange"}
    s = append(s, "grape")
    fmt.Println(s)
}
Copy after login

We can also declare the slice together when declaring variables, as shown below:

package main

import "fmt"

func main() {
    var s []string
    s = make([]string, 3)
    s[0] = "apple"
    s[1] = "banana"
    s[2] = "orange"
    s = append(s, "grape")
    fmt.Println(s)
}
Copy after login

3. Struct type

struct is a custom composite type that can store multiple types of data. The following is an example:

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{"Tom", 18}
    fmt.Println(p)
}
Copy after login

In the above example, we use the type keyword to create a struct type named Person. Then, we create a variable p of type Person and assign a value to it.

In addition, we can also use structure pointers to access fields in the struct type, as shown below:

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := &Person{"Tom", 18}
    p.Name = "Jerry"
    fmt.Println(p)
}
Copy after login

4. Interface type

Interface is an An abstract type that can be used to represent any type. The following is an example:

package main

import "fmt"

type Animal interface {
    Eat()
}

type Cat struct {
    Name string
}

func (c *Cat) Eat() {
    fmt.Printf("%s is eating
", c.Name)
}

func main() {
    var a Animal
    a = &Cat{"Tom"}
    a.Eat()
}
Copy after login

In the above example, we used the type keyword to create an interface type named Animal, which has only one method Eat. Then, we use the type keyword to create a struct type named Cat, which implements the Eat method of the Animal type. Finally, we create a variable a whose type is Animal and assign it to a variable of type Cat. When we call the Eat method of a, a sentence is output indicating that Cat is eating.

In addition, we can also use type assertions to determine whether a variable implements a certain interface, as shown below:

package main

import "fmt"

type Animal interface {
    Eat()
}

type Cat struct {
    Name string
}

func (c *Cat) Eat() {
    fmt.Printf("%s is eating
", c.Name)
}

func main() {
    var a Animal
    a = &Cat{"Tom"}
    _, ok := a.(*Cat)
    fmt.Println(ok)
}
Copy after login

In the above example, we use the type keyword to create There is an interface type named Animal, which has only one method Eat. Then, we use the type keyword to create a struct type named Cat, which implements the Eat method of the Animal type. Finally, we create a variable a whose type is Animal and assign it to a variable of type Cat. We use type assertions to determine whether a implements the Cat type and outputs true.

The above is the detailed content of How to use map, slice, struct and interface types of Golang functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template