var u []*user
используйте users := make([]*user, 0, 100000)
func RegisterUser(name string, surname string, age int)
type User struct {
Name string
Surname string
Age int
}
func RegisterUser(user User)
In a case or default clause, the last non-empty statement may be a (possibly labeled) "fallthrough" statement to indicate that control should flow from the end of this clause to the first statement of the next clause
for _, field := range fields {
if field.Name() == "fields" {
if field, ok := field.Value().(ExtField); ok {
res[field.Key] = field.Text
}
}
}
package main
import (
"io"
"os"
"os/exec"
"strings"
)
func main() {
resulution := "640x480" // Разрешение видео
framerate := "1" // Частота кадров, здесб нам достаточно 1 кадр в секунду
outfile := "out.webm" // Файл, в который писать, расширение задаст его формат
color1 := "0xFF0000" // RGB (255,0,0)
color2 := "0x00FF00" // RGB (0,255,0)
color3 := "0x0000FF" // RGB (0,0,255)
//////////////////////////////
filtergraph := []string{
"color=c=" + color1 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v1]",
"color=c=" + color2 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v2]",
"color=c=" + color3 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v3]",
`[v1] [v2] [v3] concat=n=3 [v]`,
}
allOptions := []string{
"-filter_complex", strings.Join(filtergraph, ";"),
"-map", "[v]",
"-threads", "0", "-y", outfile,
}
RunCmd("ffmpeg", allOptions...)
}
func ReadAndPrint(r io.Reader) {
io.Copy(os.Stdout, r)
}
func RunCmd(name string, args ...string) {
cmd := exec.Command(name, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
go ReadAndPrint(stdout)
go ReadAndPrint(stderr)
err = cmd.Run()
if err != nil {
panic(err)
}
}
tmpl, _ := template.ParseFiles("")
func GetMaxPoints(db *sql.DB) (int64, error) {
rows, err := db.Query(`SELECT MAX(points) FROM items`)
if err != nil {
return 0, err
}
defer rows.Close()
rows.Next()
var maxPoints int64
err = rows.Scan(&maxPoints)
return maxPoints, err
}
package main
import (
"encoding/json"
"fmt"
)
type Data1 struct {
SomeField string `json:"some_field"`
}
type Data2 struct {
SomeOtherField string `json:"some_other_field"`
}
type DefaultResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type Response1 struct {
DefaultResponse
Datas []Data1 `json:"datas"`
}
type Response2 struct {
DefaultResponse
Datas []Data2 `json:"datas"`
}
func main() {
json1 := `{"code":123,"msg":"test","datas":[{"some_field":"test field data"}]}`
json2 := `{"code":456,"msg":"test2","datas":[{"some_other_field":"other test field data"}]}`
var res1 Response1
var res2 Response2
json.Unmarshal([]byte(json1), &res1)
json.Unmarshal([]byte(json2), &res2)
fmt.Printf("%+v\n", res1)
fmt.Printf("%+v\n", res2)
}
package main
import (
"encoding/json"
"fmt"
)
type Codes1 struct {
Codes map[string][]Codes `json:"code"`
}
type Codes struct {
Filed1 int `json:"field1"`
Filed2 int `json:"field2"`
Filed3 string `json:"field3"`
}
const body = `[
{
"code": {
"Serv": [
{
"field1": 0,
"field2": 1399,
"field3": "Z",
"field4": 464,
"field5": "O",
"field6": 0,
"field7": "ASSFSW",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop",
"field16": "A",
"field17": 0
}
],
"Serv1": [
{
"field1": 0,
"field2": 1400,
"field3": "",
"field4": 464,
"field5": "O",
"field6": 0,
"field7": "ASSFSWxxx",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {
"field11.1": {
"field11.2": "N"
}
},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop2",
"field16": "A",
"field17": 121
}
]
},
"Code.field1": "0000000000",
"Code.field2": "05AUTOSP"
}
]`
func main() {
var result []Codes1
err := json.Unmarshal([]byte(body), &result)
if err != nil {
panic(err)
}
fmt.Println(result)
}
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func main() {
cfCmd := exec.Command("perl", "test.pl")
cfIn, err := cfCmd.StdinPipe()
if err != nil {
panic(err)
}
cfOut, err := cfCmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cfCmd.Start()
if err != nil {
panic(err)
}
go ProcessStdin(cfIn)
ProcessStdout(cfOut)
err = cfCmd.Wait()
if err != nil {
panic(err)
}
}
func ProcessStdin(stdin io.WriteCloser) {
defer stdin.Close()
stdin.Write([]byte("some task"))
stdin.Write([]byte("some other task"))
// ......
}
func ProcessStdout(stdout io.ReadCloser) {
defer stdout.Close()
rd := bufio.NewReader(stdout)
for {
line, err := rd.ReadString('\n')
if err != nil {
fmt.Println("Error reading stdout:", err)
os.Exit(1)
}
line = strings.TrimSpace(line)
fmt.Println("Got line from a process:", line)
}
}
$| = 1;