<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Peter Okwodu</title>
    <description>The latest articles on DEV Community by Peter Okwodu (@cephaspeter).</description>
    <link>https://dev.to/cephaspeter</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2946760%2Faf76120d-6e18-4170-8f36-eb13d44816a6.jpg</url>
      <title>DEV Community: Peter Okwodu</title>
      <link>https://dev.to/cephaspeter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cephaspeter"/>
    <language>en</language>
    <item>
      <title>Getting Started with Go on the Fly</title>
      <dc:creator>Peter Okwodu</dc:creator>
      <pubDate>Sat, 26 Apr 2025 05:55:45 +0000</pubDate>
      <link>https://dev.to/cephaspeter/getting-started-with-go-on-the-fly-26p6</link>
      <guid>https://dev.to/cephaspeter/getting-started-with-go-on-the-fly-26p6</guid>
      <description>&lt;p&gt;You can check the complete code for the sample program &lt;a href="https://github.com/jekwupeter/cli-calculator" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Go and Why It's Interesting
&lt;/h2&gt;

&lt;p&gt;Go is an interesting open-source programming language with a medium learning curve. What intrigued me about Go is that Docker and Kubernetes are written in it.&lt;/p&gt;

&lt;p&gt;Go is popular for server-side projects and writing tools because its binaries are statically linked.&lt;/p&gt;

&lt;p&gt;If you don't have Go installed, you can download it &lt;a href="https://go.dev/doc/install" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Go Packages and Modules
&lt;/h2&gt;

&lt;p&gt;Go programs are organized into packages. Packages contain a collection of code files within the same directory. Go releases a collection of packages as a module. A Go repository includes one or more modules.&lt;/p&gt;

&lt;p&gt;Let's consider building a simple two-number calculator.&lt;br&gt;
&lt;code&gt;Go init mod example.com/calculator&lt;/code&gt;&lt;br&gt;
&lt;em&gt;Go init mod&lt;/em&gt; command creates a Go module with &lt;em&gt;go.mod&lt;/em&gt; file in the current directory&lt;/p&gt;

&lt;p&gt;Create a file named &lt;em&gt;main.go&lt;/em&gt; in the directory containing your &lt;em&gt;go.mod&lt;/em&gt; file. The program's execution begins in the package main.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "log"
    "os"
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A package is identified by the word that follows the &lt;em&gt;package&lt;/em&gt; statement in the code file.&lt;/p&gt;

&lt;p&gt;You can group imports into parenthesized, “factored” import statements. I made a mistake of using curly braces and a comma; it did not build 😭&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing the getResult Function
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func getResult(x float32, op string, y float32) (float32, error){
    switch op {
    case "+":
        return operator.Add(x,y), nil
    case "-":
        return operator.Sub(x,y), nil
    case "/":
        if y == 0{
            return 0, fmt.Errorf("getResult: cannot divide by zero")
        }
        return operator.Div(x, y), nil
    case "*":
        return operator.Mult(x,y), nil
    default:
        return 0, fmt.Errorf("getResult: invalid operation")
    }   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go simplifies public and private methods by exporting only capitalized names. In the example above, the operator methods are called from a custom local module. You would also notice that the method name &lt;em&gt;getResult&lt;/em&gt; starts with a lowercase letter, meaning it should be local (package-private).&lt;/p&gt;

&lt;p&gt;When passing arguments or creating variables, the name comes before the type. Consider the string variable assignment below:&lt;br&gt;
&lt;code&gt;var name string = "Peter"&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The main Function: Program Execution
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main (){
    fmt.Println("A simple two number Calculator")

    x, err := getFloat()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    y, err := getFloat()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    op, err := getOperation()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    result, err := getResult(x, op, y)
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    fmt.Printf("The result of %v %v %v = %v", x, op, y, result)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Error Handling in Go
&lt;/h2&gt;

&lt;p&gt;You have to be careful when writing programs in Go; in the above, you may see a lot of error handling statements. &lt;a href="https://go.dev/blog/error-handling-and-go" rel="noopener noreferrer"&gt;Error handling&lt;/a&gt; in Go is handled explicitly and in a type-driven way using the error type. The developer would have to cover possible error cases—for instance, the simple calculator checks user input for a valid signed number type.&lt;/p&gt;
&lt;h2&gt;
  
  
  Managing Dependencies: Adding External Modules
&lt;/h2&gt;

&lt;p&gt;Adding a module dependency:&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;go get&lt;/em&gt; command allows us to add an external dependency on a package in the current module's directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go get golang.org/x/example/hello/reverse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Managing Dependencies: Importing Local Modules
&lt;/h2&gt;

&lt;p&gt;What if we created another module that we want to import into our current module? We could use the &lt;em&gt;go mod edit&lt;/em&gt; command to allow Go to navigate to the directory of the module to be imported.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go mod edit -replace example.com/greetings=../greetings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we use the &lt;em&gt;go tidy&lt;/em&gt; command in the directory receiving the import, which ensures that the go.mod file matches the source code in the module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go mod tidy -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running Your Go Calculator
&lt;/h2&gt;

&lt;p&gt;To run the code and be able to use the calculator, navigate to the directory of your main package file and use the &lt;em&gt;go run&lt;/em&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go run .
A simple two number Calculator
Enter a decimal number: 1
Enter a decimal number: 2
Enter any sign among the following: '+', '-', '*', '/': +
The result of 1 + 2 = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building an Executable
&lt;/h2&gt;

&lt;p&gt;To create a binary executable, you run either &lt;code&gt;go build .&lt;/code&gt; or &lt;code&gt;go install .&lt;/code&gt; in the directory with the Go main package.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;go build&lt;/em&gt; command compiles the packages and their dependencies, but it doesn't install the results. You would be able to run the executable as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./main
A simple two number Calculator
Enter a decimal number:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installing the Executable and Setting Up Environment Variables
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;go install&lt;/em&gt; command compiles and installs the packages. Running the install command in the directory of the main package will let Go compile and place the executable in the directory specified by the &lt;em&gt;GOBIN&lt;/em&gt; environment variable or its default.&lt;/p&gt;

&lt;p&gt;Use the command below to set the &lt;em&gt;GOBIN&lt;/em&gt; default directory:&lt;br&gt;
On Unix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go env -w GOBIN=/path/to/your/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go env -w GOBIN=C:\path\to\your\bin

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To unset a variable previously set by go env -w, use go env -u.&lt;/p&gt;

&lt;p&gt;Next, we add the install directory to our PATH environment variable to make running binaries easier.&lt;/p&gt;

&lt;p&gt;On Unix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export PATH=$PATH:/path/to/your/install/directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ set PATH=%PATH%;C:\path\to\your\install\directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running the Installed Binary
&lt;/h2&gt;

&lt;p&gt;This allows you run the program with just the main package file name as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ main
A simple two number Calculator
Enter a decimal number: 1
Enter a decimal number: 2
Enter any sign among the following: '+', '-', '*', '/': x
2025/04/26 05:45:16 getResult: invalid operation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This article provides a basic introduction to creating a simple command-line calculator in Go, covering module setup, program structure, basic arithmetic logic, error handling, dependency management, and building/running the application.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>cli</category>
    </item>
    <item>
      <title>Understanding the Spectrum of Typing: Static, Dynamic, and Hybrid Approaches</title>
      <dc:creator>Peter Okwodu</dc:creator>
      <pubDate>Mon, 31 Mar 2025 09:11:02 +0000</pubDate>
      <link>https://dev.to/cephaspeter/typesstring-349p</link>
      <guid>https://dev.to/cephaspeter/typesstring-349p</guid>
      <description>&lt;p&gt;I have never really understood the argument for strict typing. We store everything away as bits anyway - so why do we have type keepers? &lt;/p&gt;

&lt;h2&gt;
  
  
  The Raw truth:
&lt;/h2&gt;

&lt;p&gt;Instructions are recognized by the computer in bits, Data are stored in the computer in bytes, which are the smallest addressable memory(8 bits).&lt;/p&gt;

&lt;p&gt;Languages have different methods of managing data for safety and efficiency.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Allure of Dynamic Typing
&lt;/h2&gt;

&lt;p&gt;Developers love dynamic languages like Python as they increase their **expressivity **with its seductive duck typing. &lt;br&gt;
&lt;code&gt;x = "foo" # a string&lt;br&gt;
y = 700 # now an int - no compiler to appease&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Languages Embracing Dynamism
&lt;/h2&gt;

&lt;p&gt;Even static languages have embraced controlled dynamism over the years:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;C++ (2017):  introduced std::any and std::variant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;C# (2009):  C#4.0 added dynamic and dynamic language runtime (DLR) to handle dynamic typing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where are we?
&lt;/h2&gt;

&lt;p&gt;A hybrid approach involves static checks and inference at compile time and dynamic inference at runtime. It's about abstraction, letting the compiler handle the mundane so we can focus on the big picture.&lt;/p&gt;

&lt;p&gt;Modern languages often adopt a hybrid approach: static type checking and inference at compile time for safety, with controlled dynamism at runtime for flexibility. This abstraction lets developers delegate even more task routine checks to the compiler while focusing on higher-level logic&lt;/p&gt;

&lt;h2&gt;
  
  
  Typing Ends ...
&lt;/h2&gt;

&lt;p&gt;When coding, do you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embrace types fully? &lt;/li&gt;
&lt;li&gt;Crave the flexibility of dynamic freedom?&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>dotnet</category>
      <category>python</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
