Base64 Encode Decode in Go

Published on 29 March 2025 (Updated: 29 March 2025)

Welcome to the Base64 Encode Decode in Go page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

package main

import (
	"encoding/base64"
	"fmt"
	"os"
)

func die() {
	fmt.Println("Usage: please provide a mode and a string to encode/decode")
	os.Exit(1)
}

func main() {
	if len(os.Args) < 3 {
		die()
	}

	enc := base64.StdEncoding

	if len(os.Args[2]) == 0 {
		die()
	}

	switch os.Args[1] {
	case "encode":
		fmt.Println(enc.EncodeToString([]byte(os.Args[2])))
		return
	case "decode":
		s, err := enc.DecodeString(os.Args[2])
		if err != nil {
			die()
		}

		fmt.Println(string(s))
	default:
		die()
	}
}

Base64 Encode Decode in Go was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.