Building Web Server with Go - Part 9
· β˜• 8 min read
This is part 9 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. database Connecting to SQL database one of the common task for web applications. Let's see how we can work with SQL databases in Go. database/sql Go standard library comes with database\sql package. This provides Generic interface around SQL database. For using any sql database with Go (see list here : https://github.com/golang/go/wiki/SQLDrivers), there are 2 packages required, Generic database/sql and database driver for SQL database we want to use.

Building Web Server with Go - Part 8
· β˜• 5 min read
This is part 8 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. working with JSON data Sending and accepting JSON data is very common while building REST Services. Let's see how we can build web server in Go to work with JSON data. encoding/json To work wit JSON data we will be using encoding/json standard library package. We have two main constructs while working with JSON data.

Building Web Server with Go - Part 7
· β˜• 5 min read
This is part 7 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. Working with static files Most of the web applications need to server static file one way or another. These files may include plain html, css, js files or assets like images, audios etc. Go providers built in handler to work with static files. fileserver net/http has FileServer handler defined which returns a static file server.

Building Web Server with Go - Part 6
· β˜• 4 min read
This is part 6 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. middleware Middleware is used to provide a processing pipeline for web servers. Middleware is way of providing shared processing functionality for our web handlers. Whenever there is incoming request to our web server, we may want to log it, check if user is authorized etc. While sending response back to client, we may want to perform compressions, set some custom headers etc.

cobra
· β˜• 3 min read
cobra package cobra is command line tool that can be used to build command line tools! installation Get cobra installed go get -u github.com/spf13/cobra/cobra usage cobra init creating a new CLI application is as easy as running cobra init cobra init --pkg-name github.com/gophersumit/mycli This should create new cli application: main root main.go 1 2 3 4 5 6 7 package main import "github.com/gophersumit/mycli/cmd" func main() { cmd.Execute() } root.

Building Web Server with Go - Part 5
· β˜• 8 min read
This is part 5 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. html templates template is what it sounds like. It is a predefined html with placeholders to render data. This template is not directly returned to user, it is first parsed by a component called template engine. The work of template engine involves parsing the html and replacing the placeholders with actual data that is provided to template engine.

Building Web Server with Go - Part 4
· β˜• 8 min read
This is part 4 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. forms HTML forms are used to collect data from user. In this part, we will build simple html form that can be used to submit data and save on server. For user to be able to submit data, we will have to user html <form> tag with post action. Form tag defines other inputs which are used to collect data fields like text, dates, choices etc.

Building Web Server with Go - Part 3
· β˜• 6 min read
This is part 3 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. working with cookies Since http is stateless, various alternatives are used to maintain data between various requests and one such construct is cookie. An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with the next request to the same server.

Building Web Server with Go - Part 2
· β˜• 8 min read
This is part 2 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. http request Http request is represented by request struct in Go. This is responsible for holding all the information related to incoming http request while building server. Request struct is defined in Go Std library When an http request is received, we can extract request related information. We can extract multiple details from incoming request like

Building Web Server with Go - Part 1
· β˜• 5 min read
This is part 1 of 15 part series on building web server with go. Checkout https://www.gophersumit.com/series/web-server/ for more. net/http package net/http package comes as part of standard library of Go. This provides HTTP client and server implementations. For building web servers with Go, there is no external library required. package documentation https://golang.org/pkg/net/http/ simple web server Writing a simple web server is trivial in Go. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 package main import "net/http" func hello(w http.

flag
· β˜• 2 min read
flag package flag package provides types and functions useful for working with command line flags. installation flag package is part of standard Go library at flag usage Simple usage of flag var variable_name = flag.Type("flag_name", "initial_value", "help_message") flag.Type defines the data type of flag flag.String(), Bool(), Int(), etc. flag_name is used from command line to pass value to flag. initial_value is used to initialize flag to some default value and help_message is used to provide help information to user.

json
· β˜• 2 min read
json package json package is part of Go standard library. It providers useful types and functions that can be used to work with json data and Go data types. installation json package is part of standard Go library at encoding/json usage There are two main operation defined while working with json, Marshaling : Marshaling is conversion of Go data types to json. Unmarshaling : Converting json to Go data type.

fresh
· β˜• 1 min read
fresh package Fresh package is file watcher that rebuilds your project and provide hot-reloading like capability so that you do not have to restart web server again and again. installation go get github.com/pilu/fresh usage Go to project folder and run fresh package source and documentation https://pkg.go.dev/github.com/pilu/fresh?tab=doc