Fragment Description:



Using the go/format/parser/token packages This example illustrates how to remove a variable declaration in a Go program while maintaining correct comment association using an ast.CommentMap.


ASTparseFile_srcInterface

Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:31

/* ... <== see fragment description ... */

package main

import (
    "fmt"
    "go/parser"
    "go/token"
)

var source string

func main() {
    source = `/*Each time you set the same seed, you get the same sequence.
So of course if you're setting the seed to the time in a fast loop, you'll probably call it with the same seed many times.
In your case, as you're calling your randInt function until you have a different value, you're waiting for the time (as returned by Nano) to change.
As for all random libraries, you have to set the seed only once, for example when initializing your program unless you specifically need to reproduce a given sequence (which is usually done for debugging).
After that you simply call Intn to get the next random integer.
Moving the rand.Seed( time.Now().UTC().UnixNano()) line to the init() makes it faster.
*/
package main
import (
   "fmt"
   "math/rand"
   "time"
)
// done once
func init() {
   rand.Seed(time.Now().UTC().UnixNano())
}
func main() {
   fmt.Println(randomString(10))
}
func randomString(l int) string {
   bytes := make([]byte, l)
   for i := 0; i < l; i++ {
// just the uppercase ascii
       bytes[i] = byte(randInt(65, 90))
   }
   return string(bytes)
}
func randInt(min int, max int) int {
   return min + rand.Intn(max-min)
}`
    fset := token.NewFileSet() // positions are relative to fset
    // Parse the file containing this very example
    // but stop after processing the imports.
    f, err := parser.ParseFile(fset, "", source, parser.ParseComments)
    if err != nil {
        fmt.Println(err)
        return
    }
    // Print the imports from the file's AST.See type ast.CommandGroup
    for i, s := range f.Comments {
        fmt.Printf("comment[%d] is: %q\n=====\n", i, s.List[0].Text)
    }
    // using Text() to get a comment
    // fmt.Printf("\nfirst Comment is:\n-----\n %v\n======\n",
    // f.Comments[0].Text())
}

/* Expected Output:
comment[0] is: "Each time you set the same seed, you get the same sequence.\nSo of course if you're setting the seed to the time in a fast loop, you'll probably call it with the same seed many times.\n\nIn your case, as you're calling your randInt function until you have a different value, you're waiting for the time (as returned by Nano) to change.\n\nAs for all random libraries, you have to set the seed only once, for example when initializing your program unless you specifically need to reproduce a given sequence (which is usually done for debugging).\n\nAfter that you simply call Intn to get the next random integer.\n\nMoving the rand.Seed( time.Now().UTC().UnixNano()) line to the init() makes it faster.\n\n"
=====
comment[1] is: "//done once"
=====
comment[2] is: "//just the uppercase ascii"
=====
======
*/



Comments