Golang : When to use init() function?
When to use init()
function? If you read this official documentation at effective go init, it can be confusing for new comers to Golang.
To explain when to use init()
in the simplest term possible... you can try to think of what you need to do before launching your Go or other programs.
For example, in order for a program to execute properly in a Unix/Linux or Windows environment, you need to setup the environment variables first. Environment variables such as token, username, password, directories that are stored in the memory and retrieved by your program during execution. After the environment variables are properly configured, then your program can execute smoothly.
init()
is similar to setting up environment variables, except that it takes place inside your code. The values or states that are initialize will affect the codes behaviour until the values or states are override somewhere during execution. Hence, according to the official document - [a common use of init functions is to verify or repair correctness of the program state]
For example, in order for image package At()
and Decode()
to function properly. You need to init
the image.RegisterFormat()
function first.
Why?
Otherwise, image does not know what to expect. Is image's At()
or Decode()
functions going to deal with jpeg, gif or png files? Without the init()
, your program will throw out panic information. Which, I wish Golang will provide more description on what is causing the error rather than just throwing out panic stack information in future.
See more at https://www.socketloop.com/tutorials/golang-how-to-read-jpg-jpeg-gif-and-png-files
Another usage of init()
function is to create a default behaviour for your own program. For example, if your program extract from the environment variables and assign the values to the your own variable. If the username == '' then do what. For example, this will cause your program to exit with the message $USERNAME not set.
func init() {
if username == "" {
log.Fatal("$USERNAME not set")
}
}
So, is init()
function limited to the same file as where main()
function reside? No, you can place one init()
function in each source file.
Reference :
See also : Golang : Set, Get and List environment variables
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Golang : List all packages and search for certain package
+6.6k Golang : Individual and total number of words counter example
+22k Golang : Gorilla mux routing example
+8k Golang : Capture text return from exec function example
+5.4k Golang : Process non-XML/JSON formatted ASCII text file example
+8.1k Golang : Get curl -I or head data from URL example
+11.5k Golang : Forwarding a local port to a remote server example
+5.8k Unix/Linux : How to get own IP address ?
+6.2k Golang : Fibonacci number generator examples
+20.6k SSL : How to check if current certificate is sha1 or sha2
+8.1k Golang : Handle sub domain with Gin
+5k Unix/Linux : Get reboot history or check when was the last reboot date