Help understanding methods

Hello. I’m new to go and somewhat new to programming in general. Please forgive me if any of my nomenclature is off here. I might use a word or two incorrectly.

I’m using visual studio code to get helpful recommendations for autocomplete but I’m having a hard time understanding the type suggestions and using the documentation to figure out what data structures these methods actually want.

This is a problem more generally but I do have a specific example.

First example is pretty succinct:

It shows func OpenFile(name string, flag int, perm FileMode) (*File, error) which I take to mean that OpenFile is a method of the OS package that takes three parameters and returns two values.
The first parameter takes a string that will get assigned to a variable named “name” in the local scope of the method, the second parameter takes an int that will get assigned to a variable named “flag” in the local scope of the method, and the third parameter takes a “FileMode” data type that will get assigned to a variable named “perm” in the local scope of the method.

So first there is the question of whether I am understanding all of that correctly. If I am then I have the question of how do I figure out what the heck a “perm” is? Finally, in the example looks like this:

f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

that second argument clearly isnt an “int”, or if it is an int then I would like to understand how and why an int can look like “os.O_APPEND|os.O_CREATE|os.O_WRONLY”

Anyway sorry in advance for being a noob. I’m sure this information is out there on the internet but I’m not that good at just reading long dry manuals. Makes me nauseous. I’ll try to put back in how ever much I take out once I know this material better.

You’re mixed up here, perm is the parameter name and it is of type FileMode.

1 Like

Thanks I fixed the question. Its hard to get used to the type coming after the name. Let me see if its easier to use the documentation to figure out what a “FileMode” is.

os.O_APPEND, os.O_CREATE, and os.O_WRONLY are all constants defined in the os package. The pipe ("|") character is a binary operator just like + and - and performs a bitwise OR on its operands. This is a common pattern from the C programming language (or perhaps one of the precursors to C) to set multiple flags so instead of having a function like this:

func OpenFile(name string, append, create, readOnly, writeOnly bool, perm FileMode)

Which might need to get extra parameters added if new flags are added, which would break everyone’s code. When you call such a function, it would look like this: os.OpenFile("myfile.txt", true, true, false, true, 0644), which leaves you wondering which bool is which flag. With the bitwise operators, you can instead “layer” the flags on top of each other in a single parameter, so you get os.OpenFile("myfile.txt", os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644) which is easier to understand than having multiple Boolean flags.


You should be able to click the word, FileMode, on the site from your screenshot above and it should take you to the definition and description of that type.

1 Like

Okay that all makes perfect sense. I totally get it. Thanks so much.

Love it thank for the valuable information..