Kayıtlar

Şubat, 2022 tarihine ait yayınlar gösteriliyor

Golang working with goroutines and channels -4

Resim
 We will continue with the last part of golang reading. In our previous examples, we worked with main method which runs on only one processor and all tasks executed sequentially. But what if we need multi task environment which be scheduled by golang get results from these tasks and use them.  How about Java Future. Sounds familiar ? Most of  the Java developers uses Future interface in order to handle asynchronous processes. https://www.baeldung.com/java-future Here is the following a task executor which makes simple math calculation. public class SquareCalculator { private ExecutorService executor = Executors.newSingleThreadExecutor(); public Future<Integer> calculate (Integer input) { return executor.submit(() -> { Thread.sleep( 1000 ); return input * input; }); } } What about execution ? It is so easy to implement. After creating an instance of executor we trigger "cal...

Golang working with interfaces and functions -3

Resim
When learning Golang one of the interesting part is interfaces. It has a little bit difference with respect to other languages. Let me start with how to identify a typical interface and structs and go over. Here it is following sample interface; type area interface {     getArea () float64 } type square struct {     sideLength float64 } type triangle struct {     height float64     base   float64 } By identifying this interface we expect to implement any structs with respect to its aim. Let me more specific. GetArea method should have math definition that calculates area of received shape.  On the following the calculation square and triangle ; func (s square) getArea () float64 {     return s.sideLength * s.sideLength } func (r triangle) getArea () float64 {     return 0.5 * r.base * r.height } As it is mentioned previous reading they are receiver functions. So same function receives (not parameter) d...

Golang Receiver Functions -2

In this lesson, we will talk about receiver functions over type structures. For example, it is quite similar to the C# extensions feature, which C# users are not far from. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods If we do remember, there was an extension method that we defined as static in C#. Here, as a type safe, we add the variable of the type we specified with the "this" parameter as a method and send the content of the variable directly into the method. namespace ExtensionMethods { public static class MyExtensions { public static void PrintConsole ( this string message ) { Console.Writeln(message) } } } Main methodu static void M ain ( String[] args ) { String extensionTest = "demo"; extensionTest.PrintConsole(); } Our console application will print "demo". Golang receiver functions are also...

Welcome to Golang -1

Resim
This lesson is a introduction of Go language lesson series. First of all, you have to download golang setup in order to prepare environment. https://go.dev/dl/ So, start the installation and complete process just clicking 'Next'. Let's check simple Golang commands; In the following you will find most common commands. build -> builds one or more files. run -> first compiles one or more files and then runs. fmt -> formats the file. install -> Install packages. get -> gets the code as a resource. test -> runs test packages. Now, let's install visual studio code as the IDE and introduce Go as an extension. https://code.visualstudio.com/download After we have done our installation, we can add our extension. After clicking View-> Extension, let's add it as follows. After successfully installing our extension, let's create our go file. We can create our main.go file by showing an empty folder that we will create in Visual Studio. Let's fill our...