Skip to content

Golang: Check if a String Starts With a Prefix

Just call the HasPrefix function from the lovely strings package:

func strings.HasPrefix(s string, prefix string) bool

Simple example code to check if a list of strings has a prefix:

package main

import (

func main() {
	envList := []string{

	for _, env := range envList {
		if strings.HasPrefix(env, "env_production") {
			fmt.Println("production >", env)


Leave a Reply

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