Skip to main content

Variadic Function vs Slice Param Function in Golang #GolangDev

pixabay.com


Hello everyone, good to see you again. I am gonna share about a simple thing in fundamental Golang, this article inspired by a QnA in a website and I think I will tell the answer more flexible and wider in my personal tutorial, so this is about differentiation between Variadic Function and Slice Param in Function.


# Variadic Function


Variadic function is an unique code, this code is enable us to passing a infinite params in one function, we know that we need to put parameters in function we need to put the space in there, for example : function(param1 int, param2 string), golang absolutely doing that thing too, seems like the other languages, but Variadic function is a something new, you could put many value in function and sapparated by comma, you maybe thing about passing array in parameters but this is something different, you dont need to put parenteses when you call the function, it just like you put regular parameters but have an infinite numbers.  for example :


we could see that in parameter scope, you put a param variable that followed by three dots and data type, seems like when you defince a slice without a defining a space you put three dots to make its infinite. to call above function the code would be like this


that is variadic function. its totally unique right? but the question is, when we should use this variadic function? if you ever read about clean code, there is a rules that don't put more than 3 parameters in one function, variadic is really opposite for that rules, so I think this variadic function is only suitable in some cases, like applying in algorithm sometimes you need to put dynamic value parameters and need to reduce memory allocation in variable to push performance higher, then variadic function maybe the right option. the negative side of this function is, you only able to put one data type in one function. its mean if you put int in variadic function, so you only able to pass int on there. you can't put int and string params in one function.




# Slice Param in Function


Slice param is a regular thing that you could see in other language too, slice is an other word for array in Golang, however its there is a different thing between the two, but you could read it as array tho. the function is a regular, just like the normal function do, but you need to put a slice data type in param and the parameters could have a different data type in one function. for example :



IMHO the different between using variadic function and using slice in param function is about the functionality of the method, if you use a query and logic thing maybe put value in slice is the good option, because you need to make its as clean as possible, put it in variable is more readable. you could see above code, you can read it more clear right?.


# Conclusion 


Variadic and Slice param in Function is a different thing, and its has a spesific case to applying them, its totally depend on your code purposes, if you doing algorithm thing that need a optimal performance, maybe variadic function is the right option. and if you doing query and logic thing maybe slice is the right option. but at the end its all about your preferences, and my suggest maybe wrong on your cases. but in my experience that is the right option, and you might be consider my thought :) good luck guys

Popular posts from this blog

Automatic API Documentation Swagger in Golang #GolangDev

pixabay.com In this article, I'll explain how to generate an API blueprint instantly using SwagGo in Golang. API blueprint is a document that contains a bunch of API endpoints, its slickly same as documentation but less description, it's allow another programmer to read and see all the available endpoint and try it out with sandbox feature. Swagger is one of the most used API blueprints right now, it's available in free but limited usage. if you wanna use the free credit, you need to understand YAML notation, you can read the example notation in swagger official documentation. but again, it's really hard and takes an expensive time to arranges all the notation to achieve a good API blueprint. fortunately, there are tools in Golang that allow us to generate the YAML notation and automatically generate the blueprint page with only using markup notation, and it's FREE unlimited for self-host, insane right? SwagGo tools are available here , the documentation is very cl...

Playing with Hooks in Gorm #GolangDev

Pixabay Hello everyone, I would like to share some tricks in gorm, the Idea of this tricks is to execute a command or code statement in the middle of an ORM operation, if you ever know about trigger in the query language, then hooks is just kinda like that. Hooks could execute statement in some events such as: beforeCreate, beforeUpdate, afterCreate,  and afterUpdate . those event is similar as a trigger,  when the trigger is executed in DBMS layer, hooks are executed in the application layer. In my real case, my current company has different convention of naming database column, so when I try to implement ORM, a lot of columns such as created_at, updated_at and deleted_at can't be generated automatically. also if you have a custom UUID you could pass the value UUID generator into hooks before the insert is executed. for more detail let's jump into code : # Explanation Above code containing model struct, and several methods from gorm to serve model, TableName method used to d...

Create MVC Pattern Using Echo Framework #GolangDev

pixabay.com Hi, Back again. today I am gonna share about Go Language, yes it is a new programming language that really fast growing this day, so many startup using it, especially in indonesia, there is a Gojek, Dana, Tiket and many more. Go Lang is a programming language found by google engineer, this language is a multi paradigm language, you could use it as functional programming or treat it like an OOP bases. Echo is an framework that use Golang as a main language, to use a fast and reliable backend/web apps, as you know Golang has a really fast performance than other language, one of the reason is because its compiled not interpreted, so the Echo claim that its 10x faster. insane right ? Actually I didn't test the performance yet, but yeah for newbie like me in golang its not the main thing that you must understand, so the thing that we should know is how we actually can do with Golang.  I am person with MVC bases, before jump to golang, I have experience with La...