So far I’ve introduced Go with a “Hello World” and looked at the basics of variables. With today’s blog post I hope to take a look at the basics of functions within Go.

I am going to give examples of a few functions that I have learned and then in a future blog post expand on the concepts / build some example programs. I’m still learning Go so please bear with me.
A function is made up of:
func function_name() {
<CODE>
}
With func being a Go keyword. The main function is called upon when the Go code is compiled into an executable. Other functions can be automatically run by placing () at the end of the function, e.g.
func function_name(){
<CODE>
}()
Although this is something I am looking to write about later on when talking about concurrency. I’ve tried to comment my code where appropriate.
Date / Time
First up using some variables to hold today’s date (note: today is the 26th November 2019, expect a different date if you use the below on a different date).


replace:= strings.NewReplacer(“$”, “e”)
strings is a value. NewReplacer is a method which is tied to the value (in this case strings).
Receiving User Input

Go can be used to read input from an end user, in this case via a string of characters typed in on keyboard until the \n (enter / return) key is pressed. Before discussing this example I need to point out the the use of the _ (underscore) on the the line:
user_input, _ := input_method.ReadString(‘\n’)

Unlike some programming languages, Go can return multiple values from an operation. The line user_input, _ := input_method.ReadString(‘\n’) is an example of this as it will return:
- The characters the user typed
- An error value
The characters the user typed are expected and wanted, even if the user typed garbage that does not equal a name. The error value is only wanted if an error has occurred. There are a few different ways to deal with the multiple value return.
Note: Do not define a variable for each return and not use the variable. As pointed out in my previous post, Go will not compile if the program contains unused variables.
Method One: The Underscore
Using an underscore _ will all the program to compile and from what I can tell Go just drops the value that should be outputted. It works, but from my readings it is messy and not the way most Go programmers would go.
Method Two: Defining and Using A Variable
The 2nd method is heading the correct way to what I believe most Go programmers would do but is not completely correct. The underscore _ is gone, the two returned values (user_input a.k.a. what the user typed and err a.k.a. the error value) are given variables to be placed into and both variables are used.
The problem here is that I have used fmt to output the err value, which when I have investigated further is frowned upon as a better way is available.

Method Three: Using Log
Go has a module called “log” that can be used in conjunction with error messages and seems to be the preferred method of handling an error.

But why “err != nil” you may ask. Well, this goes back to the multiple returned values. Even when it has a success Go will return an error value, which in the case of a success is “nil” so “!=” can be used to say:
If a value not-equal to nil is returned then log the value. If nil is returned then continue with the rest of the program.
With all the above in mind the code ends up looking like:
You must be logged in to post a comment.