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

Laravel Tips : Membuat Model, Migration dan Controller dengan Sekali Jalan

  php artisan adalah generator laravel yang berfungsi untuk membuat file kodingan dengan mudah, seperti membuat controller, model, seeder, migration dan masih banyak lagi. artisan hanya bisa di lakukan di dalam lingkungan console, seperti cmd dan terminal. berikut akan saya tunjukan cara membuat controller, model, migration menggunakan php artisan. # membuat controller ketikkan perintah di bawah ini php artisan make:controller BlogController # membuat model ketikkan perintah di bawah ini php artisan make:model Blog # membuat migration ketikkan perintah di bawah ini php artisan make:migration blog # membuat seeder ketikkan perintah di bawah ini php artisan make:seeder BlogSeeder # membuat migration, controller, dan model sekaligus ketikkan perintah di bawah ini php artisan make:model -crm Blog perintah di atas akan mengenerate controller dengan nama BlogController dengan keadaan Resource method tertulis, m...

OpenVPN Alternatif Solusi Menanggulangi Blokir dari KOMINFO

Sejak akhir juli 2022 kemaren indonesia di gegerkan dengan tindakan kominfo atas diblokirnya banyak platform digital karna tidak mendaftar PSE, terutama paypal dan plaform game (steam, epic game dll). keputusan ini semakin membuat netizen geram dikarenakan platform judi online malah disetujui untuk beroperasi karna sudah daftar PSE. Jujur menurut saya memang keputusan ini sejak awal saya dengar sudah mengada-ada karna sangat aneh memblokir semua platform digital yang jumlahnya tidak terhitung banyaknya dan hanya membolehkan akses platform yang sudah mendaftar yang jumlahnya hanya ratusan.  saya sebagai developer juga merasa aneh banget ini kebijakan apalagi platform development juga banyak yang di blacklist oleh kominfo seperti, github, gitlab, bitbucket, even website CDN pun di blacklist. astaga wkwkwk. anyway terlepas dari kebijakan konyol ini, saya mau berbagi tips cara agar tetep bisa mengakses semua platform tersebut tanpa ada halangan, yaitu dengan menggunakan VPN. banyak ban...

Menghapus Virus !My Picture dan Virus ShortCut dengan 2x klik

Hallo semua sahabat blogger!!!! wahwah, sudah lama banget nggak ngeblog nih, dari mulai awal Ujian Nasional tinggat SMA sampai sekarang kuliah. :D jadi nggak enak nih mau share tapi malah di sibukin sama kegiatan dan tugas kuliah. mumpung sekarang lagi bebas-bebas nya kegiatan kuliah, saya nyempetin ngasih tutorial + script buat menghilangkan virus ShortCut dan Virus !My Picture. sebelum menginjak ke langkah-langkahnya, saya akan memberikan ciri-ciri laptop atau PC yang sudah terinfeksi oleh dua virus ini. # berikut ciri-ciri dari virus Shortcut : 1. setiap kali flashdisk di colokin ke komputer atau laptop , semua isi/file yang ada di dalam flashdisk tersebut akan berubah menjadi shortcut semua. 2. ada software yang bernama "WSCRIPT.EXE" yang di jalankan oleh user. (cara melihatnya bisa melalui task manager) 3. apabila anda menggunakan antivirus SMADAV , tiap kali flashdisk di tancapkan smadav akan langsung mendeteksi adanya virus dan semua file dalam jendela ...