Golang study Excerpt (ii)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang Study II

  1. Full Slice expressions slicing operation

    • Python A[low:high:direction]

      For example A = [1, 2, 3], a[1:2:-1] Negative 1 means the opposite direction, the result is [3,2]

    • Golang The third is not a direction: the a[low:high:max],string type slice does not support max operations.

      The max parameter is used to specify the capacity of the returned slice, slice is an array at the bottom of the Golang, and has a default initial size cap (a). After you specify the max parameter, the slice underlying array returned is no longer the default size, but the max-low size.

      So there are the following conventions: 0 <= low <= high <= max <= Cap (a)

      A: = [5]int{1, 2, 3, 4, 5}

      T: = A[1:3:9]

      At this point the slice t type is []int, length is 2, capacity is 9-1 equals 8. It means that the T-bottom array length is 8 and the subsequent append operation on t does not have to reassign the address as long as the size does not exceed 8.

  2. Type assertions

    The type of Go is somewhat special, V, OK: = x. (T) asserts that X is not nil and that the X type is T. If the type is correct, OK is the value of True,v for x converted to T. If it fails, OK is false,v is the zero_value of type T.

  3. Getting started with co-process

    If you have Python skills, coroutine is familiar with the association, understand goroutine minutes. Goroutine and coroutine Two words long so like, because it is the same thing. Do not understand is also OK, first look at the association process.

    Greenlet:python framework, very primitive, is a purely manual co-process framework

      • From Greenlet import Greenletdef test1 ():    print    gr2.switch ()    print 34def test2 ():    print    Gr1.switch ()    Print 78gr1 = Greenlet (test1) GR2 = Greenlet (test2) #创建两个协程test1, test2, but did not start the Gr1.switch () #切换到gr1, That is, start the process Gr1

    Create two co-processes (the Greenlet framework is called Greenlet), respectively, Test1,test2, but not two. Then the main coprocessor executes Gr1.switch (), the CPU execution is given to the coprocessor test1, outputs 12, then switches to the co-test2 output 56, then the CPU execution is then cut back to test1, output 34. Because this does not switch to test2,78 will not be output, the program ends.

    The above is the start of the process, you can see Greenlet all the switching work must be performed by the code display, the framework does not automatically dispatch, so that Greenlet is a purely manual framework. There is a maximum difference between a co-process and Multithreading: If a co-process does not hand over the CPU using a switch, the other process cannot get the CPU. Inside a thread, there are a lot of threads, and when the thread gets the CPU, the switch in that thread comes in (gets the CPU) but does not have a switch out (to hand over the CPU) to get the CPU, and the other coprocessor can only wait for its switch to hand over the CPU usage.

    Gevent is higher than the greenlet of the advanced framework, said before, Greenlet of the co-operation of Pure manual, original, then Gevent is advanced: Gevent will automatically create a main process, the level is high, we call the scheduler, It will automatically dispatch many other threads within the thread, when a certain process block, forcing back the CPU use, to the other non-blocking of the association, if all the other association is blocked, it will take the CPU use, polling other processes, to see who does not block, and then to whom. Gevent can allow programmers to write multi-threaded as the same way, switching, scheduling, the framework is done, and the interface is similar to multithreading, learning difficulty.

  4. Goroutine

    The former said that the Golang Goroutine Library is more advanced gevent, but also automatic scheduling, want to goroutine synchronization will use channel or other mutex, and multithreaded programming very similar.

    Unbuffered channel can be used to synchronize Goroutine

    The buffered channel can be used as a counting semaphore, which can be referenced in the previous Golang study article on the semaphore limit flow example

    Sync is available in the sync package. Mutex or sync. Rwmutex Two kinds of mutual exclusion elements. The straightforward point is the lock, get the lock, release the lock so that the synchronization

    Sync. Once is used to ensure that a bunch of goroutine execute the same function, only once, and other blocks until the end of the execution.

    var a stringvar once sync. Oncefunc Setup () {    a = "Hello, World"}func doprint () {    once. Do (Setup)    print (a)}func Threeprint () {    go doprint ()    go doprint ()    go Doprint ()}

    Although there were 3 Goroutine to execute setup, once. Do guarantee that only one goroutine will be executed once setup, the other goroutine wait for it to complete, and then print 3 times.

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.