Skip to content

Commit

Permalink
work on chapter 09
Browse files Browse the repository at this point in the history
  • Loading branch information
MKaczkow committed Mar 6, 2024
1 parent 19c1708 commit 662863e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions learning_go/09_modules_packages_imports/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.DEFAULT_GOAL := run

fmt:
go fmt ./...
.PHONY: fmt

lint: fmt
golint ./...
.PHONY: lint

vet: fmt
go vet ./...
.PHONY: vet

build: vet
go build rand_and_crand.go
.PHONY: build

run: vet
go run rand_and_crand.go
.PHONY: run
10 changes: 10 additions & 0 deletions learning_go/09_modules_packages_imports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@
- version of dependencies
- (optional) replace
- (optional) exclude
* `pkg` (for actual code) and `cmd` (for executables)

### Packages
* `Capitalized` names are exported
* `interal` package is only available to packages directly above it and it's subpackages

### API
* `alias` is different name for `type` (basically)

### Imports
3 changes: 3 additions & 0 deletions learning_go/09_modules_packages_imports/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.20
23 changes: 23 additions & 0 deletions learning_go/09_modules_packages_imports/rand_and_crand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
)

func main() {
r := seedRand()
fmt.Println(r.Int())
}

func seedRand() *rand.Rand {
var b [8]byte
_, err := crand.Read(b[:])
if err != nil {
panic("cannot seed with cryptographic random number generator")
}
r := rand.New(rand.NewSource(int64(binary.LittleEndian.Uint64(b[:]))))
return r
}

0 comments on commit 662863e

Please sign in to comment.