Go 1.8RC3 Source Code learning: Scanner

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Objective

The scanner package contains data structures and methods related to the Golang lexical analyzer, and the source code is located in <go-src>/src/go/scanner

Example_test.go

Example_test.go contains an example method that uses the scanner package to perform a lexical scan of the Euler formula

func ExampleScanner_Scan() {    // src is the input that we want to tokenize.    src := []byte("cos(x) + 1i*sin(x) // Euler")    // Initialize the scanner.    var s scanner.Scanner    fset := token.NewFileSet()                      // positions are relative to fset    file := fset.AddFile("", fset.Base(), len(src)) // register input "file"    s.Init(file, src, nil /* no error handler */, scanner.ScanComments)    // Repeated calls to Scan yield the token sequence found in the input.    for {        pos, tok, lit := s.Scan()        if tok == token.EOF {            break        }        fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)    }}

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.