本文整理汇总了Golang中github.com/cyfdecyf/bufio.NewScanner函数的典型用法代码示例。如果您正苦于以下问题:Golang NewScanner函数的具体用法?Golang NewScanner怎么用?Golang NewScanner使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewScanner函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseConfig
// overrideConfig should contain options from command line to override options
// in config file.
func parseConfig(rc string, override *Config) {
// fmt.Println("rcFile:", path)
f, err := os.Open(expandTilde(rc))
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Config file %s not found, using default options\n", rc)
} else {
fmt.Println("Error opening config file:", err)
}
return
}
defer f.Close()
IgnoreUTF8BOM(f)
scanner := bufio.NewScanner(f)
parser := reflect.ValueOf(configParser{})
zeroMethod := reflect.Value{}
var lines []string // store lines for upgrade
var n int
for scanner.Scan() {
lines = append(lines, scanner.Text())
n++
line := strings.TrimSpace(scanner.Text())
if line == "" || line[0] == '#' {
continue
}
v := strings.Split(line, "=")
if len(v) != 2 {
Fatal("config syntax error on line", n)
}
key, val := strings.TrimSpace(v[0]), strings.TrimSpace(v[1])
methodName := "Parse" + strings.ToUpper(key[0:1]) + key[1:]
method := parser.MethodByName(methodName)
if method == zeroMethod {
Fatalf("no such option \"%s\"\n", key)
}
// for backward compatibility, allow empty string in shadowMethod and logFile
if val == "" && key != "shadowMethod" && key != "logFile" {
Fatalf("empty %s, please comment or remove unused option\n", key)
}
args := []reflect.Value{reflect.ValueOf(val)}
method.Call(args)
}
if scanner.Err() != nil {
Fatalf("Error reading rc file: %v\n", scanner.Err())
}
overrideConfig(&config, override)
checkConfig()
if configNeedUpgrade {
upgradeConfig(rc, lines)
}
}
示例2: initDomainList
func initDomainList(domainListFile string, domainType DomainType) {
var err error
if err = isFileExists(domainListFile); err != nil {
return
}
f, err := os.Open(domainListFile)
if err != nil {
errl.Println("Error opening domain list:", err)
return
}
defer f.Close()
directList.Lock()
defer directList.Unlock()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain == "" {
continue
}
debug.Printf("Loaded domain %s as type %v", domain, domainType)
directList.Domain[domain] = domainType
}
if scanner.Err() != nil {
errl.Printf("Error reading domain list %s: %v\n", domainListFile, scanner.Err())
}
}
示例3: loadSiteList
func loadSiteList(fpath string) (lst []string, err error) {
var exists bool
if exists, err = isFileExists(fpath); err != nil {
errl.Printf("Error loading domaint list: %v\n", err)
}
if !exists {
return
}
f, err := os.Open(fpath)
if err != nil {
errl.Println("Error opening domain list:", err)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
lst = make([]string, 0)
for scanner.Scan() {
site := strings.TrimSpace(scanner.Text())
if site == "" {
continue
}
lst = append(lst, site)
}
if scanner.Err() != nil {
errl.Printf("Error reading domain list %s: %v\n", fpath, scanner.Err())
}
return lst, scanner.Err()
}
示例4: initDirectList
func initDirectList() {
var exists bool
var err error
if exists, err = isFileExists(configPath.alwaysDirect); err != nil {
errl.Printf("Error loading direct domain list: %v\n", err)
}
if !exists {
return
}
f, err := os.Open(configPath.alwaysDirect)
if err != nil {
errl.Println("Error opening domain list:", err)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain == "" {
continue
}
directList.Domain[domain] = domainTypeDirect
}
if scanner.Err() != nil {
errl.Printf("Error reading domain list %s: %v\n", configPath.alwaysDirect, scanner.Err())
}
}
示例5: loadUserPasswdFile
func loadUserPasswdFile(file string) {
if file == "" {
return
}
f, err := os.Open(file)
if err != nil {
Fatal("error opening user passwd fle:", err)
}
r := bufio.NewReader(f)
s := bufio.NewScanner(r)
for s.Scan() {
addUserPasswd(s.Text())
}
f.Close()
}