Home > Backend Development > Golang > Why can't I call the server method directly instead of just adding intermediate variables?

Why can't I call the server method directly instead of just adding intermediate variables?

WBOY
Release: 2024-02-09 09:36:11
forward
1168 people have browsed it

Why cant I call the server method directly instead of just adding intermediate variables?

In PHP, why can't we call the server method directly instead of just adding intermediate variables? This is a common question. PHP editor Apple will answer it for you. In PHP, we usually use front-end pages to interact with back-end servers. Directly calling server methods may involve some security risks, such as being used by hackers to perform malicious operations. To increase security, we introduce the concept of intermediate variables. By sending the front-end request to the server, and the server then performs corresponding operations based on the value of the intermediate variable, potential security risks can be effectively reduced. In addition, using intermediate variables can also increase the maintainability and scalability of the code, making the code clearer and easier to understand. In summary, adding intermediate variables is a safe and reliable practice that can improve the quality and reliability of your code.

Question content

I am writing a client for mqtt. I wrote a callback for mqtt.OnConnectHandler. I want to write the connection address of the print server.

var connectHandler mqtt.OnConnectHandler = func(c mqtt.Client) {
    
        // this is ok 
    options := c.OptionsReader()
    s := options.Servers()
        fmt.Printf("conn: %s success\n", s)

        // --------------------------------------
        // But it won't work like this
        // here have error: On servers。
        // cannot call pointer method Servers on mqtt.ClientOptionsReadercompilerInvalidMethodExpr

    o := c.OptionsReader().Servers()    
    fmt.Printf("conn: %s success\n", o)


}
Copy after login

So, I found that I can't use it directly. Why? Aren't they the same thing? Why can only intermediate variables be used to receive?

I tried two methods. You only succeed once. I thought both would be fine.

Solution

This is because the Servers method is declared with a pointer receiver. This means that in order to call Servers, the receiver must be addressable:

https://www.php.cn/link/753a043674f0193523abc1bbce678686

Temporary variables are not addressable. here:

c.OptionsReader().Servers()
Copy after login

c.OptionsReader() returns an unaddressable value, so you cannot call Servers with that value as the receiver. When you assign the return value of c.OptionsReader to a variable, it becomes addressable, and you can call Servers() with that variable.

The above is the detailed content of Why can't I call the server method directly instead of just adding intermediate variables?. For more information, please follow other related articles on the PHP Chinese website!

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