Gopher Coding Logo

Gopher Coding

Go, Docker, DevOps & Distributed Systems
  • Tutorials
  • About

How to Write Data to CSV in Go

In Go, you can use the standard library package encoding/csv [docs] to write data to a CSV file. Below is a example that shows you how you can write a slice of user-data related strings to a CSV file. The code creates a new file called users.csv and writes a slice of records to it. Finally, the Flush method is used to flush any buffered data to the underlying io.Writer, which is the file.

#csv#write#encoding#flush#struct#slice#users#people#file

Top Golang Books for Learning in 2023

1. Go Programming Language Google’s Go team member Alan A. A. Donovan and Brian Kernighan, co-author of The C Programming Language, provide hundreds of interesting and practical examples of well-written Go code to help programmers learn this flexible, and fast, language. View on Amazon 2. Learning Go: An Idiomatic Approach to Real-World Go Programming Go is rapidly becoming the preferred language for building web services. While there are plenty of tutorials available that teach Go’s syntax to developers with experience in other programming languages, tutorials aren’t enough.

#books#learning#golang#author#2023#learn

Deploy a Go App to Railway

After using Heroku for many years, I’ve recently taken a look into Railway as an alternative hosting platform for various side projects. These projects are often written in Go (of course!) so I thought I’d write a quick guide on how to setup a new project - the good news is, it’s easy! 1) Get an Account It’s easy to setup, the link on their home page will help you. Signup to Railway

#railway#heroku#hosting#app#repo#project#echo#variable#setup

Add Swagger Docs to a Go API

Swagger allows developers to easily create api documentation which is defined using comments in the code and then generated as a yaml config file. In this post we’ll be using it alongside Go (golang). Using tools like Swagger has many advantages, like allowing you to generate documentation automatically (saving you time) and that it keeps your code and documentation as close as possible (in distance terms). The idea being that if the docs are hard to update and far away, you just won’t.

#swagger#documentation#generate#cmd#tool#install#middleware#swag

Get/Set HTTP Headers in Go Request

HTTP headers, we all need ’em 😉 Here’s how you can get and set these headers within your Go requests. These include request coming into your router handlers and requests you are sending out to other systems through net/http. This can be thought of as being the same as reading headers from a request and creating new ones. First we’ll start with reading them from the request.

#http#headers#set#get#net#request#handler#router

Print Current Memory Usage

In this post we show how you can print memory usage in golang. We do this by outputting the current state of memory at any given time to show how much ram has been allocated and gc cycles made. We have created a function PrintMemUsage() to help out, so you can call this when ever you need to know. All the info we need can be acquired through the runtime [docs] package, which allows us to read the state of the memory into the MemStats struct. It returns stats like how much memory the program is using, how much of it the OS has allocated to it and the number of garbage collections.

#memory#stats#runtime#package#stdlib#memstats#alloc#garbage#gc#os

Change Go Version in Mod

Picking up an old Go project, we wanted to update the desired version it should run on (mainly so when we deployed to our live systems it would use this version too). To do this, we updated the version within the go.mod file - the file which keeps track of both versions and packages used by the project. We used the command below:

#mod#version#project#edit#packages#heroku#directive

How to Die Dump in Go - dd()

Having come from the PHP community, we often have a handy debug function at our disposal dd() (part of Laravel) and var_dump() (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go. We’re helped out by being able to use both variable parameters (shown with ...) and the interface type to handle different types, both strings and integers in our examples.

#debug#exit#dd#vardump#print#variable#php#debugging

Get Status Code from HTTP Request

We won’t go into too much detail about HTTP status codes themselves, but in this post we will talk about how to use the status code after making a request, how to check them as a range and how to print them as text. This is often important so we can check if something was successful or failed. You can always get this data if you have a net/http/Response type (spec).

#http#status#code#response#struct#range#net#error#checking

Convert an io.ReadCloser to String

Convert an io.ReadCloser to String

Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings. First we’ll look at the problem, then we have two different solutions.

#readcloser#io#convert#string#conversion#ioutil#buffer#memory#http#net
page -= 1 page += 1 page := 3
Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2024 Gopher Coding
Code Licenced under MIT Licence
Gopher Coding Logo