Friday, May 17, 2024
HomeGolangWhat does this syntax imply in go? - Getting Assist

What does this syntax imply in go? – Getting Assist


How/when are these variables used?

I got here throughout one instance right here:

fmt.Println(base64.StdEncoding.EncodeToString([]byte(“person:move”)))

base64 is a bundle
StdEncoding is a variable
EncodeToString is a operate.

I’ve a common understanding what is going on right here and what the tip result’s, but when I needed to write this myself, I might be caught since I dont perceive the small print.

Hello, @vinayak.vg, Are you able to elaborate extra on what you imply by not understanding the small print? Your description of what’s occurring sounds right to me.

Within the meantime, I’ll make an assumption that you’re referring to how a lot “stuff” is going on in a single line of code right here in comparison with how a lot you might really feel that there ought to be. I used to write down code like this, myself, maybe to a better extent in C#, however then I began including intermediate variables simply in order that I may give issues names, like this:

username := "person"
password := "move"
rawBasicAuthString := username + ":" + password
base64Encoding := base64.StdEncoding
encodedBasicAuthString := base64Encoding.EncodeToString([]byte(rawBasicAuthString))
fmt.Println(encodedBasicAuthString)

I feel when you break the expression aside, it turns into clearer what’s occurring.

If this isn’t what you imply, then please let me know.

1 Like

Thanks @skillian for the reason.
I’m making an attempt to know some fundamentals right here.
once I kind base64, the intellisense in vscode kicks in and I get solutions for strategies, variables, fixed contained in the base64 bundle.
i see that there are 4 variables on this bundle (RawStdEncoding, RawURLEncoding, StdEncoding, URLEncoding). All of them counsel me that I can use the identical capabilities following the variable (intellisense kicks in once more). I wish to know what these variables imply and what’s their significance.

Tldr: I perceive solutions for strategies and constants. I dont perceive the variable half.

The variables are defined within the docs:

The variables within the bundle are similar to variables in your personal code. You may, for instance, use base64.NewEncoding to create your personal base-64 alphabet:

bundle primary

import (
    "encoding/base64"
    "fmt"
    "os"
)

var myQueryEncoding = base64.NewEncoding("1234567890-qwertyuiopasdfghjklzxcvbnm~_QWERTYUIOPASDFGHJKLZXCVBN")

func primary() {
    fmt.Prinln(myQueryEncoding.EncodeToString(os.Args[1]))
}

Whenever you kind in myQueryEncoding., you get the identical solutions as VS Code would provide you with if you kind in base64.StdEncoding., as a result of they’re each variables with the identical sorts (*base64.Encoding), and they also have the identical strategies.

1 Like

Good @skillian . Thanks a ton. I kinda bought drifted from the basics whereas studying extra superior matters. There’s motive they’re known as “variables”. There are a number of methods to realize encoding and we are able to use what we wish based mostly on our wants, earlier than we are able to name the EncodeToString operate. Thanks once more

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments