Detect if Code is Running On Windows (at Runtime)

On of the advantages of using Go is it’s cross-platform support, the ability to write the code once and have it work across many operating systems. However, there are times when you’ll need to change the route through your code based which OS is running it. If you’re needing to do this on a large scale there are better, more manageable ways, but for tiny changes this works fine.

This snippet uses the GOOS flag within the runtime package, which is also used at compile time.

package main

import (

func main() {

    if runtime.GOOS == "windows" {
        fmt.Println("You are running on Windows")
    } else {
        fmt.Println("You are running on an OS other than Windows")


Leave a Reply

Your email address will not be published. Required fields are marked *