package main
import (
"context"
"io"
"os"
"github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/oto"
)
func playMP3(mp3Path string) error {
f, err := os.Open(mp3Path)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
d, err := mp3.NewDecoder(f)
if err != nil {
return err
}
c, err := oto.NewContext(d.SampleRate(), 2, 2, 8192)
if err != nil {
return err
}
defer func() { _ = c.Close() }()
p := c.NewPlayer()
defer func() { _ = p.Close() }()
if _, err := io.Copy(p, d); err != nil {
return err
}
return nil
}
os.CreateTemp("", "")
.jpg
. package main
import (
"bytes"
_ "embed"
_ "image/jpeg"
"io"
"log"
rand2 "math/rand"
"os"
"time"
"github.com/gen2brain/beeep"
)
//go:embed img.jpg
var imgBody []byte
func main() {
...
f, err := os.CreateTemp("", "")
if err != nil {
log.Fatalln(err)
}
defer func() {
_ = os.Remove(f.Name())
}()
_, err = io.Copy(f, bytes.NewReader(imgBody))
if err != nil {
log.Fatalln(err)
}
err = f.Close()
if err != nil {
log.Fatalln(err)
}
...
for range time.Tick(time.Second * 1) {
...
err := beeep.Notify("Батя", orders[rand], f.Name())
...
}
}
import (
"embed"
...
)
//go:embed templates
var templateFiles embed.FS
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
...
tmpl, err := template.ParseFS(templateFiles, "templates/index.html")
...
})
...
}
2. Соглашения по именованию, в разных языках они свои. https://go.dev/doc/effective_go#mixed-caps