Go is an open source programming language that makes it easy to build simple, reliable, and efficient software

Go language features

  • Simple, fast and safe
  • Parallel, fun, open source
  • Memory management, array security, fast compilation

Go language usage

Go is designed as a system programming language for large central servers with Web servers, storage clusters, and the like.

Go is arguably more efficient than most other languages in the world of high performance distributed systems. It provides a huge amount of parallel support, which is great for game server development.

Go Environment Configuration

Click to download the installation package corresponding to your own system. Different systems have different installation methods. This time, use Windows system.

Install After the installation, open the command window and check whether the installation is successful. Enter the following command on the command line

$  go version
  // go version go1.16.3 windows/amd64
Copy the code

If the following information is correctly entered, the installation is complete.

If this is not displayed correctly, you may need to manually configure the environment variable. For example, if my installation directory is C: Program Files\Go\bin, configure this path to the path environment variable.

The development tools

GoLand is highly recommended, but most of the fees are not acceptable. If you like free software, you can turn to VS Code and use it with some plug-ins.

HelloWorld

If you don’t write Hello World, you don’t really know how to get started, so as usual, Hello World

Create a project using GoLand and create a new main.go file with the following contents

package main

import "fmt"

func main(a) {
   fmt.Println("Hello, World!")}Copy the code

Enter it on the command line

$ go run main.go
 
 #  Hello, World!

Copy the code

You see Hello World, perfect, you’ve got it.

Basic grammar

Line separator

In Go, every sentence doesn’t have to end with a semicolon like in Java, which the compiler does for us, perfect for people who are obsessive-compulsive and don’t like useless symbols in their code.

But if you want to write more than one statement in a row, you must add a semicolon

  • The correct way to write it
fmt.Println("I want to learn to Go.")

fmt.Println("Let's start with Hello World.")

Copy the code
  • Wrong way to write it
fmt.Println("I want to learn to Go.") fmt.Println("Let's start with Hello World.")

Copy the code

annotation

Comments are very important in daily development. Some projects are so long that you may not remember the code you wrote after a period of time. This is when comments come into play.

Comments are critical, but you can’t rely too much on them. Having a good naming style is the best comment

Comments are not compiled

/** This is a multi-line comment */
func main(a) {
   // This is a one-line comment
   fmt.Println("I want to learn to Go.")
   fmt.Println("Let's start with Hello World.")}Copy the code

The standard naming method basically does not require comments, which can write some special cases at the time

func abc(a) string {
   // This is a method to get the user name
   return "Zhang"
}

func getUserName(a) string {

   return "Zhang"
}

Copy the code

identifier

Identifiers are used to name variables, types, and other program entities. They must start with a letter or underscore (_) and cannot contain numbers or keywords.

// This is correct
var a string = ""
var a123 string =""
var _a string = ""
// These are errors
var 1a string = ""
var case string =""

Copy the code

function

The functions inside Go are declared by func

Func function_name([parameter list]) [return_types] {function body}Copy the code

It is basically similar to the Java language, consisting of names, parameters, function bodies, and return types. The only difference is that Go can return multiple values

package main

import "fmt"

func swap(x, y string) (string, string) {
  return y, x
}

func main() {
  a, b := swap("Go", "Java")
  fmt.Println(a, b)
}
Copy the code

The result is Go,Java

Conditional statements

grammar

If Boolean expression {/* Executes when Boolean expression is true */}Copy the code

The instance

package main

import "fmt"

func main(a) {
 if 1 < 20 {
    
      fmt.Printf(Less than 20 \ n "1")}}Copy the code

The Go conditional statement can be unbracketed