Change interface value by reference

王林
Release: 2024-02-13 23:10:09
forward
848 people have browsed it

Change interface value by reference

php editor Baicao is here to introduce a method to change the interface value through reference. In programming, sometimes we need to change the parameters of a function or method, but we do not want to directly return a new value, but want to modify the original value through a reference. In this case, we can use reference parameters. By referencing parameters, we can modify the value of the passed variable directly inside the function without returning a new value. This approach improves performance and makes the code more concise and readable. In the next article, we will explain in detail how to use reference parameters to change interface values.

Question content

package main

import (
    "fmt"
)

// -------- library code. can't change ------------
type client struct {
    transport roundtripper
}

type roundtripper interface {
    do()
}

type transport struct{}

func (d transport) do() {}

var defaulttransport roundtripper = transport{}

// -------- my code. can change ------------
func changetransport(r roundtripper) {
    if r == nil {
        fmt.println("transport is nil")
        r = defaulttransport
    }
}

func main() {
    c := client{}
    changetransport(c.transport)
    fmt.println(c.transport)
}
Copy after login

Output:

transport is nil
<nil>
Copy after login

expected:

transport is nil
{}
Copy after login
Copy after login

Playground

I also tried this based on https://stackoverflow.com/a/44905592/6740589:

func changetransport(r roundtripper) {
    if r == nil {
        fmt.println("transport is nil")
        d, ok := defaulttransport.(transport)
        if !ok {
            log.fatal("impossible")
        }

        if t, ok := r.(*transport); ok {
            t = &d
            fmt.println("ignoreme", t)
        } else {
            log.fatal("uff")
        }

    }
}
Copy after login

Output:

transport is nil
2009/11/10 23:00:00 Uff
Copy after login

Playground

Solution

Use the pointer of the roundtripper interface as the changetransport function parameter to change the value of the pointer:

// -------- my code. can change ------------
func changetransport(r *roundtripper) {
    if r != nil && *r == nil {
        fmt.println("transport is nil")
        *r = defaulttransport
    }
}

func main() {
    c := client{}
    changetransport(&c.transport)
    fmt.println(c.transport)
}
Copy after login
transport is nil
{}
Copy after login
Copy after login

Playground

The above is the detailed content of Change interface value by reference. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
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