當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Match用法及代碼示例

GO語言"path"包中"Match"函數的用法及代碼示例。

用法:

func Match(pattern, name string)(matched bool, err error)

Match 報告 name 是否與 shell 模式匹配。模式語法是:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-/ characters
	'?'         matches any single non-/ character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

匹配需要模式來匹配所有名稱,而不僅僅是一個子字符串。當模式格式錯誤時,唯一可能返回的錯誤是ErrBadPattern。

例子:

package main

import (
	"fmt"
	"path"
)

func main() {
	fmt.Println(path.Match("abc", "abc"))
	fmt.Println(path.Match("a*", "abc"))
	fmt.Println(path.Match("a*/b", "a/c/b"))
}

輸出:

true <nil>
true <nil>
false <nil>

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Match。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。