What does this syntax mean in go?

How/when are these variables used?

I came across one example here:

fmt.Println(base64.StdEncoding.EncodeToString([]byte(“user:pass”)))

base64 is a package
StdEncoding is a variable
EncodeToString is a function.

I have a general understanding what is happening here and what the end result is, but if I had to write this myself, I would be stuck since I dont understand the details.

Hi, @vinayak.vg, Can you elaborate more on what you mean by not understanding the details? Your description of what’s happening sounds correct to me.

In the meantime, I will make an assumption that you are referring to how much “stuff” is happening in a single line of code here compared to how much you may feel that there should be. I used to write code like this, myself, perhaps to a greater extent in C#, but then I started adding intermediate variables just so that I can give things names, like this:

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

I think once you break the expression apart, it becomes clearer what’s going on.

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

1 Like

Thank you @skillian for the explanation.
I am trying to understand some fundamentals here.
when I type base64, the intellisense in vscode kicks in and I get suggestions for methods, variables, constant inside the base64 package.
i see that there are 4 variables in this package (RawStdEncoding, RawURLEncoding, StdEncoding, URLEncoding). All of them suggest me that I can use the same functions following the variable (intellisense kicks in again). I want to know what these variables mean and what is their significance.

Tldr: I understand suggestions for methods and constants. I dont understand the variable part.

The variables are explained in the docs:

1 Like

The variables in the package are just like variables in your own code. You could, for example, use base64.NewEncoding to create your own base-64 alphabet:

package main

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

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

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

When you type in myQueryEncoding., you get the same suggestions as VS Code would give you when you type in base64.StdEncoding., because they’re both variables with the same types (*base64.Encoding), and so they have the same methods.

1 Like

Perfect @skillian . Thanks a ton. I kinda got drifted from the fundamentals while learning more advanced topics. There is reason they are called “variables”. There are multiple ways to achieve encoding and we can use what we want based on our needs, before we can call the EncodeToString function. Thanks again

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.