github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/os/executable_procfs.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux || netbsd || (js && wasm) 6 // +build linux netbsd js,wasm 7 8 package os 9 10 import ( 11 "errors" 12 "runtime" 13 ) 14 15 func executable() (string, error) { 16 var procfn string 17 switch runtime.GOOS { 18 default: 19 return "", errors.New("Executable not implemented for " + runtime.GOOS) 20 case "linux", "android": 21 procfn = "/proc/self/exe" 22 case "netbsd": 23 procfn = "/proc/curproc/exe" 24 } 25 path, err := Readlink(procfn) 26 27 // When the executable has been deleted then Readlink returns a 28 // path appended with " (deleted)". 29 return stringsTrimSuffix(path, " (deleted)"), err 30 } 31 32 // stringsTrimSuffix is the same as strings.TrimSuffix. 33 func stringsTrimSuffix(s, suffix string) string { 34 if len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix { 35 return s[:len(s)-len(suffix)] 36 } 37 return s 38 }