Golang Reading notes

Source: Internet
Author: User

Introduced

The go language is a programming language that makes code sharing easier. The go language comes with tools that make it easier to use a package that someone else has written, and it's easier to share your own packages.

The go language support for concurrency is one of the most important features of this language. Goroutine is much like a thread, but it consumes far less memory than a thread, and requires less code to use it. Channel is a built-in data structure that allows users to synchronously send messages of the same type between different goroutine.

Terms

goroutine is a function that can be executed in parallel with other Goroutine and is also executed in parallel with the main program (the portal of the program).

Personal understanding: The equivalent of multithreading in other languages

Channel A data structure that enables secure data communication between goroutine.

You can avoid shared memory access issues that are common in other languages.

Duck Type if it barks like a duck, it could be a duck.

Specification

    • If the interface type contains only one method, then the name of the type er ends with

The type system of the Go language

    1. Simple Type
    2. The Go interface models a set of behaviors

A blank identifier (_) is used to discard values that you do not want to continue to use, such as assigning an empty name to the imported package, or ignoring values returned by the function that you are not interested in.
The init function Each package can contain any number of init functions, and all init functions discovered by the compiler are executed before the main function .

Document

Command line get

Go Doc FMT

Browse documents

godoc-http=:6060
Contains all go standard libraries and your gopath under Go source code documents

The document of the function is written directly before the function declaration, using human-readable sentences. If you want to write a document with a larger text volume, you can include a file called Doc.go in the project, use the same package name, and add the description of the package to the package name declaration.

Data type

Reference type slices, map, channel (channels), interfaces, functions, when declaring variables of the above types, the variables created are called Header values
array fixed length. A contiguous block for storing an element of the same type
If the array length is used ... instead, the number of array elements is more initialized to determine the length of the array

// 声明一个包含5个元素的整型数组var array [5]int// 用具体值初始化每个元素array := [5]int {1, 2, 3, 4, 5}// 容量由初始化值的数量决定array := [...]int {1, 2, 3, 4, 5}// 用具体值初始化索引为1,2 的元素,其余则保持0array := [5]int {1: 1, 2: 2}

Slice dynamic arrays, which can grow and shrink automatically as needed.

On a 64-bit architecture machine, a slice requires 24 bytes of memory: The pointer field requires 8 bytes, and the length and capacity fields require 8 bytes respectively.

// 创建一个字符串切片,其长度和容量都是5个元素slice := make([]string, 5)// 长度为3个元素,容量为5个元素 不容许创建容量小雨长度的切片slice := make([]int, 3, 5)// 创建 nil 整型切片var slice []int

If [] a value is specified in the operator, then an array is created instead of a slice

//创建有3个元素的整型数组array := [3]int {10, 20, 30}//创建长度和容量都是3的整型切片slice := []int {10, 20, 30}
    • Assignment and slicing
      Creating a new slice is just a partial cut out of the underlying array.
//创建一个整型切片,其长度和容量都是5个元素slice := []int{1,2,3,4,5}//创建一个新切片,其长度为2个元素,容量为4个元素newSlice := slice[1:3]

For slices with the underlying array capacity K slice[i:j]
Length: J-i
Capacity: K-i

    • Slice growth
      appendAlways increase the length of new slices, while capacity changes, capacity changes depend on the usable capacity of the slices being manipulated

    • The 3rd index When you create a slice
      The third index can be used to control the capacity of new tiles, not to increase capacity, but to limit capacity

      slice[i:j:k]
      Length J-i
      Capacity K-i

    • Iteration
      rangeReturns a value of two. The first value is a copy of the value of the second corresponding position element of the index

Map
Stores a series of unordered key-value pairs

Custom Types

    1. Create a struct type using the keyword struct
type User struct {    ID string }
    1. Type description based on an existing type as a new type
type Duration int64// int64类型叫作Duration的基础类型。Go 并不认为 Duration 和 int64 是同一类型。这两个类型是完全不同的有区别的类型。

struct

structs have 2 methods, value methods, and pointer methods

    • A copy of this value is used when the value method is called to execute
    • A reference to this value is used when a pointer method is called to execute

      You can also use pointers to call value methods, and the compiler converts the code
      (*obj).method()
      You can also use a value to invoke a reference method, and the compiler converts the code
      (&ojb).method()

Whether you use a value method or a pointer method when declaring a method, depending on whether you want a new value or modify the original value

Interface
Used to define behavior

Channel

unbuffered := make(chan int)    //无缓冲通道buffered := make(chan int, 10)
    • A channel without a buffered channel that has no ability to hold any value before it is received

Summarize

  • The go language is modern, fast, with a powerful standard library.
  • The go language has built-in support for concurrency.
  • The go language uses interfaces as the base module for code reuse.
  • Each code file belongs to a package, and the package name UK has the same name as the folder where the code file resides
  • The go language provides a variety of ways to declare and initialize variables. If the value of the variable is not explicitly initialized, the compiler initializes the variable to a value of 0.
  • Use pointers to share data between goroutine between functions.
  • Complete concurrency and synchronization by starting Goroutine and using channels
  • The Go language provides built-in functions to support data structures within the go language.
  • Go language package is the basic unit of organization Code
  • The environment variable Gopath determines where the go source code is saved, compiled, and installed on disk.
  • You can set up different gopath for the Elegy project to keep the source code and dependent isolation.
  • The Go tool is the best tool for working on the command line
  • Developers can use go get to get someone else's package and install it into their Gopath specified directory
  • It's easy to create a package for someone else, just put the source code in the common code base and follow some simple rules.
  • The go language is designed to share code as the core feature and driving force of the language.
  • It is recommended to use dependency management tools to manage dependencies such as GODEP, Vender, GB
  • Array structure is the cornerstone of slices and maps
  • In the go language, slices are often used to work with collections of data, and map is used to process data with an almost structure.
  • Built-in function make allows you to create slices and maps and specify the original length and capacity. You can also use slices and map literals directly, using the literal initial values alive.
  • Tiles have a capacity limit, but you can use the built-in append function to expand capacity.
  • Map growth has no capacity or any limitations
  • Built-in function Len can be used to get the length of a slice or map
  • Built-in function caps can only be used for slices (the CAP () function returns the amount of space allocated by the array slice)
  • By combining, you can create multidimensional groups and multidimensional slices. You can also use slices or other maps as values for the map. But slices cannot be used as keys to maps
  • Passing a slice or map to a function is very inexpensive and does not duplicate the underlying array structure.
  • You can declare a user-defined type by using the keyword struct or by specifying a type that already exists.
  • Method provides a way to increase the behavior of a user-defined type.
  • The nature of the type of person to be designed is primitive or non-primitive.
  • An interface is a type that declares a set of behaviors and supports polymorphism.
  • An embedded type provides the ability to extend a type without using inheritance.
  • Identifiers are either public in the package or not disclosed in the package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.