Go programming language: The Ultimate Beginner's Guide to Learn Go Programming Step by Step by lnc mEm & Bach John

Go programming language: The Ultimate Beginner's Guide to Learn Go Programming Step by Step by lnc mEm & Bach John

Author:lnc, mEm & Bach, John [lnc, mEm]
Language: eng
Format: epub
Published: 2021-01-04T16:00:00+00:00


The main topics covered in this chapter are as follows:

- Write comments.

- Libraries and import.

- Functions.

- Data types.

- Named return values.

- Variables and memory.

- Control sentences.

- Generate functions.

- Deferred implementation.

- Interfaces.

- Multiple inputs.

- Error handling.

- Simultaneous implementation.

- Web

Write comments:

To write a one-line comment

// single line comment

Type a comment with more than one line

/ * Multi-

line comment * /

Libraries and import

Each source file starts with the keyword packag. The main keyword is used to identify the file as an operational file, not a library.

package main

To import an office package into the file, we use the Import instruction in the following way:

import (

"fmt" // package in the standard language library

"io / ioutil" // I / O functions are applied

m "math" // We use the letter m to shorten the name of the mathematical functions library

"net / http" // web server

"os" // functions at the operating system level such as handling file s

"strconv" // Text conversions

)

Functions

Functions are defined by the word func followed by the function name.

The main function is private, and is the entrance to the program's executable file (the Go language uses ornate braces {} to define parts / blocks).

func main () {

// To output text on the main output unit (stdout), we use the Println function in the fmt library

fmt.Println ("Hello world!")

// Call a function from the same current package

beyondHello ()

}

Functions need parentheses that receive Parameters, and even in the absence of parameters, parentheses are required.

func beyondHello () {

// Variable declaration (variable must be declared before using it)

var x int

// Give value to the variable

x = 3

// Short definition using: = Includes variable definition, specifying its type and giving it valu e

y: = 4

// A function that returns two separate values

sum, prod: = learnMultiple (x, y)

// Print and output in a simple and direct

fmt.Println ("sum:", sum, "prod:", prod)

learnTypes ()

}

The function definition can have multiple coefficients and return values. For example, learnMultiple takes the coefficients x and y and returns two sum and prod values ​​of type Int.

func learnMultiple (x, y int) (sum, prod int) {

// Separate the returned values ​​with a regular comma

return x + y, x * y

}

Data Types

func learnTypes () {

// Short tariffs usually perform the desired purpose

// Define a text variable using a double quotation mark

str: = "Learn Go!"

// Define a text variable using a single quotation mark

s2: = `A" raw "string literal can include line breaks.`

// Define a variable of type rune which is another name for the type int32 and the variable of this type contains unicode

g: = 'Σ'

// Float

f: = 3.14195

// Definition of Complex Number (Complex)

c: = 3 + 4i

// Define variables using var

var u uint = 7 // natural number (positive integer)

var pi float32 = 22. / 7 // A decimal of 32 bits

// Short definition (byte is another name for uint8)

n: = byte ('\ n')

// Arrays have a fixed and fixed size at compile time

// Defines an int array of 4 elements with an initial value of zero

var a4 [4] int

// Define an array of 3 elements with values ​​3, 1 and 5

a3: = [.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.