<?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: Daniel Porter</title>
    <description>The latest articles on DEV Community by Daniel Porter (@danielporter000866901).</description>
    <link>https://dev.to/danielporter000866901</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%2F1030524%2F68a75661-5f26-416e-bcab-36057a475ec0.png</url>
      <title>DEV Community: Daniel Porter</title>
      <link>https://dev.to/danielporter000866901</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danielporter000866901"/>
    <language>en</language>
    <item>
      <title>Golang Web App Demonstration</title>
      <dc:creator>Daniel Porter</dc:creator>
      <pubDate>Wed, 05 Apr 2023 00:38:12 +0000</pubDate>
      <link>https://dev.to/danielporter000866901/golang-web-app-demonstration-256i</link>
      <guid>https://dev.to/danielporter000866901/golang-web-app-demonstration-256i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Today, I will be demonstrating in a series of videos how to build a very basic web app using Golang. The app I will be demonstrating is very basic, and it does close to nothing, but it does highlight the major ideas in web app creation. Without further ado, let's jump right in!&lt;/p&gt;




&lt;h2&gt;
  
  
  Routing and Middleware
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Xmu3zPmqRTQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Handlers
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vHyr7-Bwn3M"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Form Handlers
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/DCZuNLBap0o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Database access
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/6dT4LUEi-VI"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;a href="https://github.com/golang/go/wiki/SQLDrivers"&gt;Golang Database Drivers&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  HTML Templates
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;With rendering and building, as well as models.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/P-tTx3jIlv0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gowebexamples.com/templates/"&gt;Go Template Cheat Sheet&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Styling and images
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Sy3nNXRuejA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Writing to log files
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qkB0ixZ4Ogc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/O3qv7YWmxC4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>go</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Golang Web App Development</title>
      <dc:creator>Daniel Porter</dc:creator>
      <pubDate>Thu, 23 Feb 2023 02:39:22 +0000</pubDate>
      <link>https://dev.to/danielporter000866901/golang-web-app-development-4d6f</link>
      <guid>https://dev.to/danielporter000866901/golang-web-app-development-4d6f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Go, or Golang, is a statically typed programming language released by Google in 2009. It is a backend programming language, which I am using to build a web app. It has extensive built-in packages and features, as well as the capability of adding third party packages. Some of these include &lt;strong&gt;&lt;a href="https://pkg.go.dev/net/http" rel="noopener noreferrer"&gt;net/http&lt;/a&gt;&lt;/strong&gt;, which includes the ability to listen on a port, route URLs and serve back content, &lt;strong&gt;&lt;a href="https://pkg.go.dev/html/template" rel="noopener noreferrer"&gt;html/template&lt;/a&gt;&lt;/strong&gt;, which has the ability to create Go templates out of HTML pages and dynamically passed data, and &lt;strong&gt;&lt;a href="https://pkg.go.dev/database/sql" rel="noopener noreferrer"&gt;database/sql&lt;/a&gt;&lt;/strong&gt;, which provides the ability to connect to a database. &lt;/p&gt;

&lt;p&gt;Using these packages, along with others, a developer can build a web app in Go.&lt;/p&gt;

&lt;p&gt;Go has great &lt;strong&gt;&lt;a href="https://pkg.go.dev/std" rel="noopener noreferrer"&gt;documentation &lt;/a&gt;&lt;/strong&gt; on the language and their packages, which I highly recommend checking out.&lt;/p&gt;

&lt;p&gt;Below, I will demonstrate some of the abilities listed above, as well as some other interesting “quirks” about Go.&lt;/p&gt;




&lt;h2&gt;
  
  
  Structure
&lt;/h2&gt;

&lt;p&gt;All files must begin with a package declaration in the first line.&lt;br&gt;
To run a program, it will need a main function, like Java's &lt;code&gt;main(String[] args)&lt;/code&gt; method.&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

func main() {
  // contents of the main function here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Declarations
&lt;/h2&gt;

&lt;p&gt;From a Java perspective, much of Golang can appear backwards. For example, in Java, declaration looks like:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8ps4ib9vm2atavqm4sf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8ps4ib9vm2atavqm4sf.png" alt="Java string and array" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As opposed to Go's...&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5uohz0p936comm5ksyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5uohz0p936comm5ksyj.png" alt="Golng string and slice" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go is very strict when it come to the "use it or loose it" mentality. If a variable or import is declared but not used, it must be deleted, or it will not compile.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhzuab1hpvy8iwxfad0e5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhzuab1hpvy8iwxfad0e5.png" alt="Can't compile unused variables" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Speaking of declarations, here are some examples of common declarations in Go. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjossrzmvsn8oqzjkbsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjossrzmvsn8oqzjkbsp.png" alt="Go declarations" width="295" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, developers can declare variables using the syntax above, but there are other ways.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// declare with type
var myString string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// declare with implied type
var myString = "Hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This last one can only be used inside a block, such as a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// declare with ':='
myString := "Hello world"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Using Packages
&lt;/h2&gt;

&lt;p&gt;To import a package, simply declare &lt;code&gt;import()&lt;/code&gt;, and list the imported packages in the parenthesis. &lt;/p&gt;

&lt;p&gt;To use functions and variables, write the package name, followed by &lt;code&gt;.&lt;/code&gt; and the name of the thing you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// For example

import (
  "fmt"
)

fmt.Print("Hello World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, use it or loose it. The file will not compile with unused imports.&lt;/p&gt;

&lt;p&gt;To import third-party packages as well as other local packages, you will need to create a go.mod file. In the command line, type &lt;code&gt;go mod init {{project name}}&lt;/code&gt; to create the go.mod file.&lt;/p&gt;




&lt;h2&gt;
  
  
  Loops
&lt;/h2&gt;

&lt;p&gt;While languages such as Java and JavaScript have for, while, and do…while loops, Go has one: the for loop. It is very easy to work with, but there are multiple ways of using it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The basic for loop:
for i := 0; i &amp;lt; 10; i++ {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The foreach loop (slices and arrays):
for index, item := range mySlice {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The foreach loop (maps):
for key, value:= range myMap {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The foreach loop (channels):
for input := range myChannel {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The while true loop:
for isTrue {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The infinite loop:
for {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Access
&lt;/h2&gt;

&lt;p&gt;In Java, developers mark attributes and methods as &lt;em&gt;private&lt;/em&gt; or &lt;em&gt;public&lt;/em&gt; to control access. Go also allows developers to control access when exporting packages, but not with &lt;em&gt;public&lt;/em&gt; or &lt;em&gt;private&lt;/em&gt;, but with case. If a function or variable begins with an &lt;em&gt;uppercase letter&lt;/em&gt;, it is public. Otherwise, it is private.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var privateString = "no one can see this"
var PublicString = "other packages can see this."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Objects &lt;em&gt;(sort of)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Golang is not an &lt;em&gt;Object Oriented Programming&lt;/em&gt; language, but it does allow for functionality similar to object creation. This can be done by declaring custom types, based on the &lt;em&gt;struct&lt;/em&gt;, short for structure. Functions can even have &lt;em&gt;receivers&lt;/em&gt;, making them like methods, the &lt;code&gt;(u *User)&lt;/code&gt; in this case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl84957ah4t7jo2mnvbia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl84957ah4t7jo2mnvbia.png" alt="Golang custom type" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With receivers, the function must be attached to a variable of the type defined in the receiver place &lt;code&gt;(u *User)&lt;/code&gt;. In the above example, one must declare a variable of type User and call the function from that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myUser User
myUser.WriteName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the functionality, the receiver is referenced with the variable &lt;code&gt;u&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Arrays and Slices
&lt;/h2&gt;

&lt;p&gt;We are all familiar with arrays. In languages like Java, the size of arrays are set, while in JavaScript, the size is dynamic. Golang has both. &lt;em&gt;Arrays&lt;/em&gt; have a set length, which can not be changed. A &lt;em&gt;slice&lt;/em&gt; is similar to an array, but it has variable length. They look very similar, but with one key difference: the value in the square brackets during declaration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1n0i7tkj8pmdqg19xoi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1n0i7tkj8pmdqg19xoi.png" alt="Golang array and slice declaration" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A developer can also return a slice from the contents of an array/slice using index values in square brackets, returning all elements from the first listed index to the last (exclusive). The absence of a starting index means start at 0, and the absence of an ending index means end at the last element (inclusive).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// return elements at indexes [2,5)
newSlice := myArray[2:5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// return elements at 2 to the end
newSlice := myArray[2:]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// return elements from the start to 5 (exclusive)
newSlice := myArray[:5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Random numbers
&lt;/h2&gt;

&lt;p&gt;A big drawback of Golang is its random number generation. Using the &lt;strong&gt;&lt;a href="https://pkg.go.dev/math/rand" rel="noopener noreferrer"&gt;math/rand&lt;/a&gt;&lt;/strong&gt; package, there is a series of random value generators, but they all use the same seed, so the same numbers will be return in the same order each  time. To combat this, the developer must add a seed of his own. To get a unique seed, developers use the current date in the &lt;strong&gt;&lt;a href="https://pkg.go.dev/time" rel="noopener noreferrer"&gt;time&lt;/a&gt;&lt;/strong&gt; package, as that is always changing; thus, a different seed each time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rand.NewSource(time.Now().UnixMicro())
randomInt := rand.Intn(10)
// return a number [0, 10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Functions work in Go the very similarly to Java and JavaScript. Declare with the &lt;code&gt;func&lt;/code&gt; keyword, give the name, the parameters, the return and in the braces, give the functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func equals (arg1 int, arg2 int) bool {
  return arg1 == arg2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where Go gets interesting is with the receivers, talked about under &lt;strong&gt;Objects&lt;/strong&gt;, and with the ability to have multiple return values.&lt;br&gt;
These are put in parentheses after the parameters. The return statement must return both types listed, and when the return is being handled, both values must be accounted for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func RandomString(x int, n int) (string, error)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Handle string and error return values
output, err := util.RandomString(3, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is very common for functions in Go to return multiple values, one usually being an &lt;code&gt;error&lt;/code&gt;, a type similar to Java's Exceptions.&lt;/p&gt;

&lt;p&gt;Golang will not compile with unused variables, so what do developers do if a function has multiple return values, but they only care about one? In that case, developers use the &lt;code&gt;_&lt;/code&gt; character. It means "I know a value is here, but I don't care about it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Ignore second return value
output, _ := util.RandomString(3, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Pointers
&lt;/h2&gt;

&lt;p&gt;One thing to watch out for is pass-by-value. Golang is &lt;em&gt;entirely&lt;/em&gt; pass-by-value. This means every value you pass as an argument in a function will be copied into a new variable in a new place in memory. &lt;/p&gt;

&lt;p&gt;In other words, a variable passed as an argument will not be changed by the function, as the function is manipulating a different variable in a different place in memory.&lt;/p&gt;

&lt;p&gt;Read this article on &lt;strong&gt;&lt;a href="https://david-yappeter.medium.com/golang-pass-by-value-vs-pass-by-reference-e48aac8b2716#:~:text=Pass%20by%20value%20will%20pass,created%20one%20into%20the%20method." rel="noopener noreferrer"&gt;pass-by-value&lt;/a&gt;&lt;/strong&gt; for a better description than I can give.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7q3i9ymm22ejthvaamc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7q3i9ymm22ejthvaamc.gif" alt="Pass-by-value demonstration" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers can counteract this is by using pointers. Below, I demonstrate how to tell a function to expect a pointer and how to get a variable’s pointer.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;*&lt;/code&gt; before a type tells Go to expect a pointer to a value of the given type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Make a function that expects a pointer to a string and returns a pointer to an intager.
func usePointer (s *string) *int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get a pointer to a variable, use the &lt;code&gt;&amp;amp;&lt;/code&gt; character before the variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myString := "Hello World"
usePointer(&amp;amp;myString)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Reference Types
&lt;/h2&gt;

&lt;p&gt;Now that I've told you that Go is &lt;em&gt;entirely&lt;/em&gt; pass-by-value, let me tell you about the times it isn't.&lt;/p&gt;

&lt;p&gt;Go really is entirely pass-by-value, but there are some datatypes known as reference types.&lt;br&gt;
The &lt;strong&gt;&lt;a href="https://david-yappeter.medium.com/golang-pass-by-value-vs-pass-by-reference-e48aac8b2716#:~:text=Referenced%20Data%20Type" rel="noopener noreferrer"&gt;article&lt;/a&gt;&lt;/strong&gt; from &lt;strong&gt;Pointers&lt;/strong&gt; talks about this.&lt;/p&gt;

&lt;p&gt;These are types that don't directly store values (which is what value types do), but they instead store pointers. This means, even if the variable is copied, it still holds inside it a pointer to the actual value.&lt;/p&gt;

&lt;p&gt;With reference types, developers do not need to get a pointer from them, as they &lt;em&gt;contain&lt;/em&gt; a pointer. They can be used as normal, no pointer required!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr5ou3j4k9gh71kg6tu9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr5ou3j4k9gh71kg6tu9.png" alt="Value and Reference types" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Routing
&lt;/h2&gt;

&lt;p&gt;Using the &lt;strong&gt;&lt;a href="https://pkg.go.dev/net/http" rel="noopener noreferrer"&gt;net/http&lt;/a&gt;&lt;/strong&gt; package in the &lt;code&gt;main()&lt;/code&gt; function, developers can route URLs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.HandleFunc("/URL", (func(res http.ResponseWriter, req *http.Request) {
        // handle responce and requests for /URL here
    }))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HanldeFunc requires a URL pattern in a string and a handler function that has &lt;code&gt;http.ResponseWriter&lt;/code&gt; and &lt;code&gt;*http.Request&lt;/code&gt; parameters. The function can be stored in another file or package, but it must be properly formatted and imported.&lt;/p&gt;

&lt;p&gt;Another option for routing is to store the routes in another file or package using http.ServeMux.&lt;br&gt;
You must make sure that the Mux variable is accessible to the listener (see &lt;strong&gt;Listening&lt;/strong&gt;) by either making the variable public, or by making a public function that returns a pointer to the Mux variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mux := http.NewServeMux()
Mux.HandleFunc("/URL", (func(res http.ResponseWriter, req *http.Request) {
        // handle responce and requests for /URL here
    }))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Returning Go Templates
&lt;/h2&gt;

&lt;p&gt;Developers can make Go templates using HTML pages. They can be simple static pages or complex dynamic pages. That topic is too big for this blog post, but I will demonstrate how to parse and return the file in a handle function (See &lt;strong&gt;Routing&lt;/strong&gt;) using the &lt;strong&gt;&lt;a href="https://pkg.go.dev/html/template" rel="noopener noreferrer"&gt;html/template&lt;/a&gt;&lt;/strong&gt; package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t, _ := template.ParseFiles("templates/index.html")
t.Execute(res, nil)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the use of the &lt;code&gt;_&lt;/code&gt; and the &lt;code&gt;nil&lt;/code&gt;, which is a pointer to nothing, similar (although not the same) to the &lt;code&gt;null&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;res&lt;/code&gt; variable used is of type &lt;code&gt;http.ResponseWriter&lt;/code&gt;, the same one in the response handler.&lt;/p&gt;


&lt;h2&gt;
  
  
  Listening
&lt;/h2&gt;

&lt;p&gt;The most important part in web app development is the listener. Without it, it does not matter how advanced the app is, it is useless. This is also found in the &lt;strong&gt;&lt;a href="https://pkg.go.dev/net/http" rel="noopener noreferrer"&gt;net/http&lt;/a&gt;&lt;/strong&gt; package.&lt;/p&gt;

&lt;p&gt;This is done with a very simple command in the &lt;code&gt;main()&lt;/code&gt; function.&lt;br&gt;
&lt;em&gt;(These examples will listen on port 3000)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If using http.HanldeFunc for &lt;strong&gt;Routing&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.ListenAndServe(":3000", nil)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If using http.ServeMux for &lt;strong&gt;Routing&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mux := http.NewServeMux()
// list routing functions here

http.ListenAndServe(":3000", mux)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;These have been some of the basics of Golang with a &lt;em&gt;very&lt;/em&gt; basic overview of web app development. Thank you for taking the time to read this blog post, and I would like to again recommend the Golang &lt;a href="https://pkg.go.dev/std" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;. It is a great tool. There are also countless articles and videos about web app development. The ones I've read and seen are very good. It takes time and dedication, but it is not overly complex &lt;em&gt;(just another benefit of Golang)&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
