// An implementation of Conway's Game of Life. package main import ( "bytes" "fmt" "math/rand" "time" ) // Field represents a two-dimensional field of cells. type Field struct { s [][]bool w, h int } // NewField returns an empty field of the specified width and height. func NewField(w, h int) *Field { s := make([][]bool, h) for i := range s { s[i] = make([]bool, w) } return &Field{s: s, w: w, h: h} } // Set sets the state of the specified cell to the given value. func (f *Field) Set(x, y int, b bool) { f.s[y][x] = b } // Alive reports whether the specified cell is alive. // If the x or y coordinates are outside the field boundaries they are wrapped // toroidally. For instance, an x value of -1 is treated as width-1. func (f *Field) Alive(x, y int) bool { x += f.w x %= f.w y += f.h y %= f.h return f.s[y][x] } // Next returns the state of the specified cell at the next time step. func (f *Field) Next(x, y int) bool { // Count the adjacent cells that are alive. alive := 0 for i := -1; i <= 1; i++ { for j := -1; j <= 1; j++ { if (j != 0 || i != 0) && f.Alive(x+i, y+j) { alive++ } } } // Return next state according to the game rules: // exactly 3 neighbors: on, // exactly 2 neighbors: maintain current state, // otherwise: off. return alive == 3 || alive == 2 && f.Alive(x, y) } // Life stores the state of a round of Conway's Game of Life. type Life struct { a, b *Field w, h int } // NewLife returns a new Life game state with a random initial state. func NewLife(w, h int) *Life { a := NewField(w, h) for i := 0; i < (w * h / 4); i++ { a.Set(rand.Intn(w), rand.Intn(h), true) } return &Life{ a: a, b: NewField(w, h), w: w, h: h, } } // Step advances the game by one instant, recomputing and updating all cells. func (l *Life) Step() { // Update the state of the next field (b) from the current field (a). for y := 0; y < l.h; y++ { for x := 0; x < l.w; x++ { l.b.Set(x, y, l.a.Next(x, y)) } } // Swap fields a and b. l.a, l.b = l.b, l.a } // String returns the game board as a string. func (l *Life) String() string { var buf bytes.Buffer for y := 0; y < l.h; y++ { for x := 0; x < l.w; x++ { b := byte(' ') if l.a.Alive(x, y) { b = '*' } buf.WriteByte(b) } buf.WriteByte('\n') } return buf.String() } func main() { l := NewLife(40, 15) for i := 0; i < 300; i++ { l.Step() fmt.Print("\x0c", l) // Clear screen and print field. time.Sleep(time.Second / 30) } }
Write, Run & Share Go code online using OneCompiler's Go online compiler for free. It's one of the robust, feature-rich online compilers for Go language, running on the latest version 1.10.2. Getting started with the OneCompiler's Go compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as GO
and start coding.
OneCompiler's Go online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Go program which takes name as input and prints hello message with your name.
package main
import "fmt"
func main() {
var name string
fmt.Scanf("%s", &name)
fmt.Printf("Hello %s", name)
}
Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.
Some of the products developed using Go are Kubernetes, Docker, Dropbox, Infoblox etc.
Data type | Description | Size | Range |
---|---|---|---|
uint8 | 8-bit unsigned integer | 1 byte | 0 to 255 |
int8 | 8-bit signed integer | 1 byte | -128 to 127 |
int16 | 16-bit signed integer | 2 bytes | -32768 to 32767 |
unit16 | 16-bit unsigned integer | 2 bytes | 0 to 65,535 |
int32 | 32-bit signed integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
uint32 | 32-bit unsigned integer | 4 bytes | 0 to 4,294,967,295 |
int64 | 64-bit signed integer | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
uint64 | 64-bit unsigned integer | 8 bytes | 0 to 18,446,744,073,709,551,615 |
float32 | 32-bit signed floating point number | 4 bytes | ±1.5e-45 to ±3.4e38 |
float | 64-bit signed floating point number | 8 bytes | ±5.0e-324 to ±1.7e308 |
string | sequence of immutable text | ||
bool | Stores either true or false | 1 byte | True or false |
Variable is a name given to the storage area in order to manipulate them in our programs.
var varible-names datatype;
When ever you want to perform a set of operations based on a condition or set of conditions then If or IF-ELSE or Nested If-Elif-Else are used.
if(conditional-expression) {
// code
}
if(conditional-expression) {
// code
} else {
// code
}
if(conditional-expression) {
// code
} else if(conditional-expression) {
// code
} else {
// code
}
For loop is used to iterate a set of statements based on a condition.
for Initialization; Condition; Increment/decrement {
// code
}
Switch is an alternative to If-Else-If ladder.
switch conditional-expression {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
Go doesn't have while or do-while loops like in C.
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
func functionname(parameter-name type) returntype {
//code
}