Skip to main content

28 posts tagged with "Go"

Go Programming Language and Go Modules

View All Tags

How to Print to Standard Error in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Go program has two output streams: standard output, where fmt.Println writes, and standard error, os.Stderr, for problems. Write a warning with fmt.Fprintln(os.Stderr, ...) and it still reaches the screen when the results go into a file.

How to Read an Environment Variable in Go

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a port, a database address or an API key, belong in environment variables. Go reads one with os.Getenv("NAME"), which returns an empty string when the variable isn't set.

How to Create a Pointer to a Struct in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Go passes values by copying them, so a function that receives a struct works on its own copy. To let a function change your struct, pass a pointer to it: &Counter{} creates a struct and gives you its address in one step.

How to Pass a Function as an Argument in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

In Go, functions are values: you can store one in a variable and pass it to another function. The receiving parameter's type describes the function it accepts, such as func(int) int, a function that takes an int and returns an int.

How to Add Minutes to a Time in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

In Go, a length of time is a time.Duration, and the package gives you constants such as time.Minute to build one. t.Add(15 * time.Minute) returns the time 15 minutes later; later.Sub(t) gives the gap back.

How to Define a Struct in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A struct groups related values under one type: a book's title, author and year. In Go you declare the struct type once, then create as many values of it as you need.

How to Import Packages in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Go's standard library is a set of packages: fmt for printing, strings for text, time for dates and many more. A file lists the packages it uses in an import block and then calls their functions through the package name, as in strings.ToUpper.