Sample code:
package main import ( "fmt" "path" ) func main() { filename := "device/sdk/" filenameall := (filename) filesuffix := (filename) fileprefix := filenameall[0:len(filenameall) - len(filesuffix)] //fileprefix, err := (filenameall, filesuffix) ("file name:", filenameall) ("file prefix:", fileprefix) ("file suffix:", filesuffix) }
Execution results:
file name:
file prefix: CMakeLists
file suffix: .txt
(filename)
Get the file name that does not contain the directory
(filename)
Get file suffix
File prefixes can be obtained using slices:
fileprefix := filenameall[0:len(filenameall) - len(filesuffix)]
You can also use () to obtain:
import "strings" fileprefix, err := (filenameall, filesuffix)
Supplement: golang judges prefix suffix and inclusion relationship
HasPrefix determines whether the string s starts with prefix:
(s, prefix string) bool
HasSuffix determines whether the string s ends with sffix:
(s, suffix string) bool
String inclusion relationship
Contains determines whether the string s contains substr:
(s, substr string) bool
Test source code:
package main import ( "fmt" "strings" ) func main() { var str1 string = "This is an example of a string" var str2 string = "this is an " var str3 string = " of a string" ((str1, str2)) ((str1, str3)) }
Running results:
[root@localhost golang]# go run
false
true
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.