Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Have OS specific implementations in golang

In go, you can create separate implementations for different operating systems by creating multiple files:

package main

import (
    "play.ground/foo"
)

func main() {
    foo.HelloComputer()
}

-- go.mod --
module play.ground

-- foo/foo_windows.go --
// +build windows

package foo

import "fmt"

func HelloComputer() {
    fmt.Println("Hello windows!")
}

-- foo/foo_linux.go --
// +build !windows

package foo

import "fmt"

func HelloComputer() {
    fmt.Println("Hello not windows!")
}