本文整理汇总了Golang中bufio.Scanner.Split方法的典型用法代码示例。如果您正苦于以下问题:Golang Scanner.Split方法的具体用法?Golang Scanner.Split怎么用?Golang Scanner.Split使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bufio.Scanner
的用法示例。
在下文中一共展示了Scanner.Split方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ReadInt
func ReadInt(s *bufio.Scanner) int {
s.Split(bufio.ScanWords)
s.Scan()
result, _ := strconv.Atoi(s.Text())
return result
}
示例2: SolveMonk
func SolveMonk(scanner *bufio.Scanner) string {
var buffer bytes.Buffer
scanner.Split(bufio.ScanWords)
scanner.Scan()
trials, _ := strconv.Atoi(scanner.Text())
for i := 0; i < trials; i++ {
scanner.Scan()
nums, _ := strconv.Atoi(scanner.Text())
var tmp int
min, freq := math.MaxInt32, 0
for j := 0; j < nums; j++ {
scanner.Scan()
tmp, _ = strconv.Atoi(scanner.Text())
if tmp == min {
freq++
} else if tmp < min {
min = tmp
freq = 1
}
}
if freq%2 == 0 {
buffer.WriteString("Unlucky\n")
} else {
buffer.WriteString("Lucky\n")
}
}
return buffer.String()
}
示例3: ReadInts
func ReadInts(s *bufio.Scanner, total int) []int64 {
s.Split(bufio.ScanWords)
a := make([]int64, total)
for i := 0; i < total; i++ {
s.Scan()
a[i], _ = strconv.ParseInt(s.Text(), 10, 64)
}
return a
}
示例4: parseBody
func parseBody(scanner *bufio.Scanner, contentLength int64) []byte {
bytes := make([]byte, contentLength)
scanner.Split(bufio.ScanBytes)
var i int64
for i = 0; i < contentLength; i++ {
scanner.Scan()
bytes[i] = scanner.Bytes()[0]
}
return bytes
}
示例5: parseTicketData
func parseTicketData(s *bufio.Scanner) (price, sold float64, err error) {
s.Split(bufio.ScanLines)
if scannable := s.Scan(); scannable == false {
err = errors.New("EOL Reached; Nothing else scannable")
return
}
values := strings.Split(s.Text(), " ")
price, _ = strconv.ParseFloat(values[0], 64)
sold, _ = strconv.ParseFloat(values[1], 64)
return
}
示例6: parseHeaders
// Parses header text out of a bufio.Scanner, should be called immediately after parseStatusLine.
func parseHeaders(scanner *bufio.Scanner, method HttpMethod) []string {
headers := make([]string, 0)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
// Break if we hit a empty line.
if len(line) == 0 {
break
}
headers = append(headers, line)
}
return headers
}
示例7: getPrinter
func getPrinter(scanner *bufio.Scanner, lang language.Tag) (*message.Printer, error) {
catalog := message.DefaultCatalog
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
s := scanner.Text()
line := strings.SplitN(s, "=", 2)
if len(line) != 2 {
return nil, fmt.Errorf("Invalid line in messages file: %v", line)
}
catalog.SetString(lang, line[0], line[1])
}
if err := scanner.Err(); err != nil {
return nil, err
}
return catalog.Printer(lang), nil
}
示例8: scanMessagePart
func scanMessagePart(scanner *bufio.Scanner, sep string) (string, bool) {
scanner.Split(bufio.ScanLines)
var parts []string
for scanner.Scan() {
// TODO: consistent de-indent by first-line detection
line := strings.TrimLeft(scanner.Text(), " ")
if len(line) == 0 {
return strings.Join(parts, sep), true
}
parts = append(parts, line)
}
if len(parts) > 0 {
return strings.Join(parts, sep), true
}
return "", false
}
示例9: prepareTasks
func prepareTasks() (*[]string, error) {
var tasks []string
var scanner *bufio.Scanner
var err error
if *urlFile == "-" {
scanner = bufio.NewScanner(os.Stdin)
} else {
file, err := os.Open(*urlFile)
if err != nil {
return nil, err
}
defer file.Close()
scanner = bufio.NewScanner(file)
}
if *isJSON {
var jsonInput []byte
scanner.Split(bufio.ScanBytes)
for scanner.Scan() {
for _, b := range scanner.Bytes() {
jsonInput = append(jsonInput, b)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if err = json.Unmarshal(jsonInput, &tasks); err != nil {
return nil, err
}
} else {
for scanner.Scan() {
tasks = append(tasks, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
}
return &tasks, err
}
示例10: BinaryQueries
func BinaryQueries(scanner *bufio.Scanner) string {
var buffer bytes.Buffer
scanner.Split(bufio.ScanWords)
scanner.Scan()
length, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
lines, _ := strconv.Atoi(scanner.Text())
arr := make([]int, length)
for i := 0; i < length; i++ {
scanner.Scan()
arr[i], _ = strconv.Atoi(scanner.Text())
}
var cmd, X, R int
for i := 0; i < lines; i++ {
scanner.Scan()
cmd, _ = strconv.Atoi(scanner.Text())
if cmd == 1 {
scanner.Scan()
X, _ = strconv.Atoi(scanner.Text())
if arr[X-1] == 0 {
arr[X-1] = 1
} else {
arr[X-1] = 0
}
} else {
scanner.Scan()
_, _ = strconv.Atoi(scanner.Text())
scanner.Scan()
R, _ = strconv.Atoi(scanner.Text())
if arr[R-1] == 1 {
buffer.WriteString("ODD\n")
} else {
buffer.WriteString("EVEN\n")
}
}
}
return buffer.String()
}
示例11: readMatrix
func readMatrix(s *bufio.Scanner) [][]byte {
s.Split(bufio.ScanWords)
s.Scan()
r, _ := strconv.Atoi(s.Text())
s.Scan()
c, _ := strconv.Atoi(s.Text())
s.Split(bufio.ScanLines)
g := make([][]byte, r)
for i := 0; i < r; i++ {
s.Scan()
t := s.Text()
g[i] = make([]byte, c)
for j := 0; j < c; j++ {
g[i][j] = t[j] - '0'
}
}
return g
}
示例12: main
func main() {
var (
cst customers
c customer
ex bool
err error
text []byte
command string
flagEndofLoop bool = true
scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
fio, acc string
money float64
time int64
fioArr []string
)
// читаем файл
text, err = ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
}
//преобразовываем файл в массив структур
err = json.Unmarshal(text, &cst)
if err != nil {
fmt.Println(err)
}
for flagEndofLoop {
// пишем команду
fmt.Println("Введите команду и параметр через пробел (для более подробной информации наберите help)")
scanner.Split(bufio.ScanWords)
scanner.Scan()
command = scanner.Text()
switch command {
case "fio":
// ищем по фамилии
scanner.Split(bufio.ScanWords)
scanner.Scan()
v := scanner.Text()
fmt.Printf("%v\n", v)
fioArr = append(fioArr, string(v))
scanner.Scan()
v = scanner.Text()
fmt.Printf("%v\n", v)
fioArr = append(fioArr, string(v))
fio = strings.Join(fioArr, " ")
fio = strings.Trim(fio, " ")
ex, c = cst.FindFio(fio)
if ex {
fmt.Printf("%v\n", c)
} else {
fmt.Println("нет такого")
}
case "acc":
// ищем по счету
scanner.Split(bufio.ScanWords)
scanner.Scan()
acc = scanner.Text()
ex, c = cst.FindAccount(acc)
if ex {
fmt.Printf("%v\n", c)
} else {
fmt.Println("нет такого")
}
case "money":
// ищем по деньгам
scanner.Split(bufio.ScanWords)
scanner.Scan()
money, _ = strconv.ParseFloat(scanner.Text(), 32)
ex, c = cst.FindMoney(money)
if ex {
fmt.Printf("%v\n", c)
} else {
fmt.Println("нет такого")
}
case "time":
// ищем по сроку
scanner.Split(bufio.ScanWords)
scanner.Scan()
time, _ = strconv.ParseInt(scanner.Text(), 10, 0)
ex, c = cst.FindTime(time)
if ex {
fmt.Printf("%v\n", c)
} else {
fmt.Println("нет такого")
}
case "help":
// помощь по командам
fmt.Println("Возможные команды:")
fmt.Println("fio строка : поиск по фамилии и имени")
fmt.Println("acc строка: поиск по счету")
fmt.Println("money число: поиск по кол-ву денег")
fmt.Println("time число: поиск по сроку ")
//.........这里部分代码省略.........