go init

2026-08-30 浏览 (1)

init.go 源码

// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials  : https://learngoprogramming.com
// In-person training  : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus

package main

import (
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"time"
)

// init is another special function
// Go calls it before the main function
func init() {
	rand.Seed(time.Now().UnixNano())
	initCells()
}

// initCells initialize the played cells to empty
func initCells() {
	for i := range cells {
		cells[i] = emptyCell
	}
}

func setGameSpeed() {
	// args can be used within the if block
	// gs and err can be used in the else if and else branches
	if args := os.Args; len(args) == 1 {
		fmt.Println("Setting game speed to default.")
	} else if gs, err := strconv.Atoi(args[1]); err != nil {
		fmt.Println("Wrong game speed. Provide an integer.")
	} else {
		gameSpeed = time.Duration(gs) * time.Second
	}

	fmt.Printf("Game speed is %q.\n", gameSpeed)
}

你可能感兴趣的文章

go board_test

go play_test

go play

go turn

go board

go skin

go main

go ending_test

go ending

  • 所属分类: 后端技术
  • 本文标签: golang
  • 版权声明: 本文链接 https://seaxiang.com/blog/S4vAtJfH