<?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: Rexsy Bima Trima Wahyu</title>
    <description>The latest articles on DEV Community by Rexsy Bima Trima Wahyu (@rexsy_bimatrimawahyu_d1).</description>
    <link>https://dev.to/rexsy_bimatrimawahyu_d1</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%2F1814552%2Fa237f41e-50ac-4788-9c39-6fb83ab256b7.jpg</url>
      <title>DEV Community: Rexsy Bima Trima Wahyu</title>
      <link>https://dev.to/rexsy_bimatrimawahyu_d1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rexsy_bimatrimawahyu_d1"/>
    <language>en</language>
    <item>
      <title>Mastering Go is Easy Actually, part 2 : Understanding Go's Predeclared Types and Declarations</title>
      <dc:creator>Rexsy Bima Trima Wahyu</dc:creator>
      <pubDate>Wed, 23 Apr 2025 07:31:30 +0000</pubDate>
      <link>https://dev.to/rexsy_bimatrimawahyu_d1/mastering-go-is-easy-actually-part-2-understanding-gos-predeclared-types-and-declarations-496b</link>
      <guid>https://dev.to/rexsy_bimatrimawahyu_d1/mastering-go-is-easy-actually-part-2-understanding-gos-predeclared-types-and-declarations-496b</guid>
      <description>&lt;p&gt;Now that your development environment is set up, it's time to explore Go's language features and best practices. The guiding principle? Write code that clearly communicates your intentions. As we dive into Go's built-in types and variable declarations, you'll see how subtle differences from other languages can impact readability and functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Predeclared Types
&lt;/h3&gt;

&lt;p&gt;Go comes with several built-in types, known as predeclared types. These include familiar options like booleans, integers, floats, and strings. While they may seem straightforward, transitioning developers often need to adjust to Go's idiomatic usage. Before examining each type, let's cover some universal concepts.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Zero Value
&lt;/h4&gt;

&lt;p&gt;Like many modern languages, Go assigns a default zero value to any declared but unassigned variable. This explicit zeroing eliminates bugs common in languages like C and C++. Each type's zero value will be discussed alongside its details.&lt;/p&gt;

&lt;h4&gt;
  
  
  Literals
&lt;/h4&gt;

&lt;p&gt;Go literals represent explicitly specified numbers, characters, or strings. There are four common kinds (plus a rare fifth for complex numbers):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Integer literals&lt;/strong&gt;: Base 10 by default, with prefixes for binary (&lt;code&gt;0b&lt;/code&gt;), octal (&lt;code&gt;0o&lt;/code&gt;), and hexadecimal (&lt;code&gt;0x&lt;/code&gt;). Underscores improve readability (e.g., &lt;code&gt;1_234&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Floating-point literals&lt;/strong&gt;: Include a decimal point or exponent (&lt;code&gt;6.03e23&lt;/code&gt;). Hexadecimal floats use &lt;code&gt;0x&lt;/code&gt; and &lt;code&gt;p&lt;/code&gt; for exponents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rune literals&lt;/strong&gt;: Represent characters in single quotes, with various numeric escapes (e.g., &lt;code&gt;'\u0061'&lt;/code&gt; for 'a').&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;String literals&lt;/strong&gt;: Use double quotes for interpreted strings (processing escapes) or backticks for raw strings (no escapes).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Literals are untyped, defaulting to a type when context doesn't specify one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Booleans
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;bool&lt;/code&gt; type holds &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, with a zero value of &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Numeric Types
&lt;/h3&gt;

&lt;p&gt;Go offers 12 numeric types across three categories:&lt;/p&gt;

&lt;h4&gt;
  
  
  Integer Types
&lt;/h4&gt;

&lt;p&gt;Signed and unsigned integers range from 1 to 8 bytes (see table in original text). All have a zero value of &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Special Integer Types
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;byte&lt;/code&gt;: Alias for &lt;code&gt;uint8&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt; and &lt;code&gt;uint&lt;/code&gt;: Platform-dependent (32 or 64 bits). Integer literals default to &lt;code&gt;int&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rune&lt;/code&gt; and &lt;code&gt;uintptr&lt;/code&gt;: For Unicode code points and pointer arithmetic, respectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Choosing an Integer
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Match external formats (e.g., network protocols).&lt;/li&gt;
&lt;li&gt;Use generics for library functions.&lt;/li&gt;
&lt;li&gt;Default to &lt;code&gt;int&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Integer Operators
&lt;/h4&gt;

&lt;p&gt;Standard arithmetic (&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;) and bitwise operations (&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;). Division truncates toward zero.&lt;/p&gt;

&lt;h4&gt;
  
  
  Floating-Point Types
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;float32&lt;/code&gt; and &lt;code&gt;float64&lt;/code&gt; follow IEEE 754. Prefer &lt;code&gt;float64&lt;/code&gt; for precision. Avoid floats for exact decimals (e.g., money).&lt;/p&gt;

&lt;h4&gt;
  
  
  Complex Types
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;complex64&lt;/code&gt; and &lt;code&gt;complex128&lt;/code&gt; support complex numbers via the &lt;code&gt;complex&lt;/code&gt; function. Rarely used outside niche applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strings and Runes
&lt;/h3&gt;

&lt;p&gt;Strings are immutable, Unicode-compatible, and support concatenation (&lt;code&gt;+&lt;/code&gt;). The &lt;code&gt;rune&lt;/code&gt; type (alias for &lt;code&gt;int32&lt;/code&gt;) represents Unicode code points. Zero value is an empty string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explicit Type Conversion
&lt;/h3&gt;

&lt;p&gt;Go requires manual conversion between types, even for similar numerics. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30.2&lt;/span&gt;
&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c"&gt;// Explicit conversion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No non-boolean type can be converted to &lt;code&gt;bool&lt;/code&gt;; use comparisons instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Literals Are Untyped
&lt;/h3&gt;

&lt;p&gt;Literals adapt to compatible types but can't overflow variables (e.g., assigning &lt;code&gt;1000&lt;/code&gt; to a &lt;code&gt;byte&lt;/code&gt; errors).&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Declarations: &lt;code&gt;var&lt;/code&gt; vs. &lt;code&gt;:=&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; for zero values, package-level vars, or explicit types.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:=&lt;/code&gt; for type-inferred local variables (must declare at least one new var).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Constants
&lt;/h3&gt;

&lt;p&gt;Declared with &lt;code&gt;const&lt;/code&gt;, constants are immutable and computed at compile time. They can be typed or untyped (more flexible).&lt;/p&gt;

&lt;h3&gt;
  
  
  Naming Conventions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use camelCase, not snake_case.&lt;/li&gt;
&lt;li&gt;Short names in small scopes (e.g., &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Descriptive names at package level.&lt;/li&gt;
&lt;li&gt;Avoid underscores and Unicode look-alikes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Unused Variables
&lt;/h3&gt;

&lt;p&gt;Go enforces local variable usage (compile-time error if unused). Package-level vars and constants are exempt but discouraged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercises
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Declare an integer &lt;code&gt;i&lt;/code&gt; as 20, assign to float &lt;code&gt;f&lt;/code&gt;, and print both.&lt;/li&gt;
&lt;li&gt;Create a constant assignable to &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;float64&lt;/code&gt;, then print both.&lt;/li&gt;
&lt;li&gt;Assign max values to &lt;code&gt;byte&lt;/code&gt;, &lt;code&gt;int32&lt;/code&gt;, and &lt;code&gt;uint64&lt;/code&gt; vars, increment, and print.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;You've now mastered Go's predeclared types, declarations, and idioms. Next, we'll explore composite types like slices and maps, and revisit strings and runes in depth.&lt;/p&gt;

</description>
      <category>go</category>
      <category>itseasyactually</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Mastering Go is Easy Actually, part 1 : Setting up Go Development Environment</title>
      <dc:creator>Rexsy Bima Trima Wahyu</dc:creator>
      <pubDate>Wed, 23 Apr 2025 07:00:23 +0000</pubDate>
      <link>https://dev.to/rexsy_bimatrimawahyu_d1/mastering-go-is-easy-actually-part-1-setting-up-go-development-environment-2n02</link>
      <guid>https://dev.to/rexsy_bimatrimawahyu_d1/mastering-go-is-easy-actually-part-1-setting-up-go-development-environment-2n02</guid>
      <description>&lt;h2&gt;
  
  
  Setting Up Your Go Development Environment: A Comprehensive Guide
&lt;/h2&gt;

&lt;p&gt;Every programming language requires a proper development environment, and Go is no exception. Whether you're a seasoned Go developer or just starting out, setting up your environment correctly is crucial for a smooth coding experience. In this guide, we'll walk you through installing Go, writing your first program, and exploring essential tools that make Go development efficient and enjoyable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the Go Tools
&lt;/h2&gt;

&lt;p&gt;The first step is to download and install the Go development tools. Visit the &lt;a href="https://golang.org/dl" rel="noopener noreferrer"&gt;official Go downloads page&lt;/a&gt; and select the appropriate installer for your operating system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mac users&lt;/strong&gt;: Use the &lt;code&gt;.pkg&lt;/code&gt; installer or install via Homebrew with &lt;code&gt;brew install go&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows users&lt;/strong&gt;: Use the &lt;code&gt;.msi&lt;/code&gt; installer or install via Chocolatey with &lt;code&gt;choco install golang&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linux/BSD users&lt;/strong&gt;: Download the gzipped TAR file and install manually:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-C&lt;/span&gt; /usr/local &lt;span class="nt"&gt;-xzf&lt;/span&gt; go1.20.5.linux-amd64.tar.gz
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'export PATH=$PATH:/usr/local/go/bin'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.bash_profile
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;source&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.bash_profile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify your installation by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see output similar to &lt;code&gt;go version go1.20.5 darwin/arm64&lt;/code&gt; (Mac) or &lt;code&gt;go version go1.20.5 linux/amd64&lt;/code&gt; (Linux).&lt;/p&gt;

&lt;h2&gt;
  
  
  Your First Go Program
&lt;/h2&gt;

&lt;p&gt;Let's create the classic "Hello, World!" program to test your setup:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new directory for your project:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;ch1
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;ch1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize a Go module:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go mod init hello_world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a file named &lt;code&gt;hello.go&lt;/code&gt; with the following content:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Build and run your program:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go build
&lt;span class="nv"&gt;$ &lt;/span&gt;./hello_world
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Essential Go Tools
&lt;/h2&gt;

&lt;p&gt;Go comes with powerful built-in tools that help maintain code quality:&lt;/p&gt;

&lt;h3&gt;
  
  
  go fmt
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;go fmt&lt;/code&gt; command automatically formats your code to match Go's standard style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go &lt;span class="nb"&gt;fmt&lt;/span&gt; ./...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  go vet
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;go vet&lt;/code&gt; detects potential bugs in your code that might compile but could cause issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go vet ./...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  go run
&lt;/h3&gt;

&lt;p&gt;For quick testing without building an executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go run hello.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Choosing Development Tools
&lt;/h2&gt;

&lt;p&gt;While you can write Go code with any text editor, these IDEs offer excellent Go support:&lt;/p&gt;

&lt;h3&gt;
  
  
  Visual Studio Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Free and open-source&lt;/li&gt;
&lt;li&gt;Install the Go extension for full support&lt;/li&gt;
&lt;li&gt;Features: auto-formatting, code completion, debugging&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  GoLand
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Commercial IDE from JetBrains&lt;/li&gt;
&lt;li&gt;Comprehensive Go support out of the box&lt;/li&gt;
&lt;li&gt;Free trial available&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Go Playground
&lt;/h2&gt;

&lt;p&gt;For quick experimentation without local setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access &lt;a href="https://play.golang.org" rel="noopener noreferrer"&gt;The Go Playground&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Write and run small Go programs&lt;/li&gt;
&lt;li&gt;Share code via unique URLs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Automating with Makefiles
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;Makefile&lt;/code&gt; to automate common tasks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="nv"&gt;.DEFAULT_GOAL&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; build

&lt;span class="nl"&gt;.PHONY&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;fmt vet build&lt;/span&gt;
&lt;span class="nl"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    go &lt;span class="nb"&gt;fmt&lt;/span&gt; ./...

&lt;span class="nl"&gt;vet&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;fmt&lt;/span&gt;
    go vet ./...

&lt;span class="nl"&gt;build&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;vet&lt;/span&gt;
    go build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Staying Up-to-Date
&lt;/h2&gt;

&lt;p&gt;Go maintains strong backward compatibility. To update:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mac/Windows&lt;/strong&gt;: Re-run installer or package manager&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linux/BSD&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mv&lt;/span&gt; /usr/local/go /usr/local/old-go
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-C&lt;/span&gt; /usr/local &lt;span class="nt"&gt;-xzf&lt;/span&gt; go1.20.6.linux-amd64.tar.gz
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /usr/local/old-go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Exercises
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Run "Hello, World!" on The Go Playground and share the link&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;clean&lt;/code&gt; target to your Makefile&lt;/li&gt;
&lt;li&gt;Experiment with formatting and see how &lt;code&gt;go fmt&lt;/code&gt; responds&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;You now have a fully functional Go development environment! With these tools in place, you're ready to explore Go's features and start building robust applications. In the next chapter, we'll dive into Go's type system and variable declarations.&lt;/p&gt;

</description>
      <category>go</category>
      <category>tutorial</category>
      <category>itseasyactually</category>
    </item>
    <item>
      <title>Django Class Based View made easy</title>
      <dc:creator>Rexsy Bima Trima Wahyu</dc:creator>
      <pubDate>Sat, 12 Oct 2024 05:48:00 +0000</pubDate>
      <link>https://dev.to/rexsy_bimatrimawahyu_d1/django-class-based-view-made-easy-13hm</link>
      <guid>https://dev.to/rexsy_bimatrimawahyu_d1/django-class-based-view-made-easy-13hm</guid>
      <description>&lt;p&gt;As we all know, django uses &lt;a href="https://www.geeksforgeeks.org/django-project-mvt-structure/" rel="noopener noreferrer"&gt;MVT (model-view-template)&lt;/a&gt; for its design on developing web application. &lt;/p&gt;

&lt;p&gt;View itself is a callable which takes a request and returns a response. its more than just a function as Django provides something called Class Based View, so developer can write a view using, well, a class based approach or you can say an OOP approach. This Class Based View is designed so we can structure our views and can be reused by the power of inheritance and mixins.&lt;/p&gt;

&lt;p&gt;As documented well in &lt;a href="https://docs.djangoproject.com/en/5.1/topics/class-based-views/intro/" rel="noopener noreferrer"&gt;django docs&lt;/a&gt;, one of the problem with function based view is that there is no way to extend or customize them beyond some configuration options, limiting their usefulness in many real-world applications.&lt;/p&gt;

&lt;p&gt;The toolkit of base classes and mixins in django is designed for maximum flexibility. Lets take a look how we can use most basic Class Based View in django using View class inheritance and compare it to function based view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#views.py using View class inheritance
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IndexView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello world from indexview&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello world from indexview in post method&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#views.py function based view
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello world from index funcion view&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello world from index funcion view in post method&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you look at above, a class-based view allows you to respond to different HTTP request methods with different class instance methods, instead of with conditionally branching code inside a single view function. now imagine in each view above that we added 10 lines of logic each method, you should be can tell which one is easier to walkthrough.&lt;/p&gt;

&lt;p&gt;In order to register the class based view into the url configuration is that we must call &lt;code&gt;as_view()&lt;/code&gt; class method which will basically convert the Class View into a callable function. this converted function will call &lt;code&gt;setup()&lt;/code&gt; to initialize its attribute and then calls &lt;code&gt;dispatch()&lt;/code&gt; to check which method user have (a GET, POST, or other method) and will connect the request method to coressponding matching method the class based view originally have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#urls.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;myapp.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;IndexView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IndexView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_view&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;IndexView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;class based view also supports all http shourtcut django has, such as &lt;code&gt;render()&lt;/code&gt; function to render a template, here is a modified example of django cbv using render shortcut and cooperated with django messages framework&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IndexView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GEtindex.html&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# imagine 10 line of view logic here
&lt;/span&gt;        &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;POST Success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#add display success message here by django messages framework
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;POSTindex.html&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Overall django class based view allow developer to write better to understand view logic, the more complex a view logic is, Im pretty sure it will be harder to read if we just use function based view (too many if statement to check what method are user using for an example) and hard to scale, meanwhile django cbv is designed to break our view logic to multiple method like get and post method. and can be used again in inheritance if you want, altho i can argue that the more inheritance we have in a class it will be harder to read due to its abstraction.&lt;/p&gt;

&lt;p&gt;you can check more about class based view in django docs starts from &lt;a href="https://docs.djangoproject.com/en/5.1/topics/class-based-views/" rel="noopener noreferrer"&gt;here&lt;/a&gt;, &lt;a href="https://docs.djangoproject.com/en/5.1/topics/class-based-views/intro/" rel="noopener noreferrer"&gt;here&lt;/a&gt;, &lt;a href="https://docs.djangoproject.com/en/5.1/topics/class-based-views/generic-display/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also a good alternative docs that focuses in django class based view is &lt;a href="https://ccbv.co.uk/" rel="noopener noreferrer"&gt;ccbv.co.uk&lt;/a&gt; &amp;lt;- i use this website to know which view template logic i can use fast!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
      <category>python</category>
    </item>
  </channel>
</rss>
