Morteza R
Morteza R

Reputation: 2319

Using Go packages

I am not sure how Go packages are invoked. For example if I want to create random numbers I should import "math/random", but isn't it simply a part of the "math" library? So why doesn't this piece of code work:

package main

import(
    "fmt"
    "math"
)

func main(){
    r := rand.New(rand.NewSource(99))
    fmt.Println(r)

}

I mean, can't I just directly access the random functions by simply importing a superclass (in this case, simply the math "math" package)?

Upvotes: 1

Views: 1140

Answers (1)

Arjan
Arjan

Reputation: 21485

Thats because rand is a separate package that is hierarchically under the math package math/rand, so you have to import it specifically:

package main

import(
    "fmt"
    "math/rand"
)

func main(){
    r := rand.New(rand.NewSource(99))
    fmt.Println(r)

}

Upvotes: 6

Related Questions