Standard library / path/filepath

path/filepath.HasPrefix()

Deprecated: checks a path string prefix, not a component boundary; unsuitable for deciding containment. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func HasPrefix(p, prefix string) bool

Parameters & Attributes

NameTypeDescription
pstringPath to test.
prefixstringString prefix.

Return Value

bool

Deprecated: checks a path string prefix, not a component boundary; unsuitable for deciding containment.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()(); fmt.Println(filepath.HasPrefix("/data-old", "/data")) }
Go Reference