Go from getting started to mastering (iii) string, time, Process Control, function __go

Source: Internet
Author: User
Tags goto switch case time and date
I. Use of strings and StrConv

Strings

Strings. Hasprefix (S String,preffix string) bool:
Determines whether the string s starts with prefix

Stirngs. Hassuffix (S String,suffix string) bool:
Determines whether the string s ends with suffix

Strings. Index (s string,str string) int:
Determine where Str first appears in S, or return 1 if it does not appear

Strings. Lastindex (s string,str string) int:
To determine the last occurrence of STR in S, or to return 1 if it does not appear

Strings. Replace (str string,old string,new string,n int):
String substitution

Strings. Count (str string,count int) string:
String count

Strings. Repeat (str string,count int) string:
Repeat Count Times Str

Strings. ToLower (str string)
Convert to lowercase

Strings. ToUpper (str string) string:
Convert to uppercase

Strings. Trimspace (str string):
Remove first whitespace character from string

Strings. Trim (str string,cut string):
get rid of string and tail cut characters

Strings. Trimleft (str string,cut string):
Remove string Header cut character

Strings. TrimRight (str string,cunt string):
Remove string tail cut character

Strings. Field (str string):
Returns the slice of all substrings of the Str space-delimited string

String. Split (str string,split string):
Returns the slice of all substrings of STR split split

Strings. Join (S1 []string,sep string):
To connect all the elements in the S1 with Sep.

StrConv

Scronv. itoa (i int): Converts an integer to a string

Scronv. Atio (str string) (Int,errror):
Converts a string to an integer two, a time and date type in go

Current: now:= time. Now ()

Time. Now (). Day ()

Time. Now (). Minute ()

Time. Now (). Month ()

Time. Now (). Year ()

Time. Duration used to represent nanosecond

Some of the commonly used time constants

Const (
nanosecond Duration = 1
Microsecond =1000 * nanosecond
Millisecond =1000 * microsecond
Second =1000 * Millisecond
Minute =60 * Second
Hour =60 * Minute
)

Formatting

Package main

Import "FMT"

func Main () {
    var a int =
    //&a print A's pointer address
    FMT. Println (&a)
    //define a pointer type variable p
    var p *int
    //Speak a pointer address copy to p
    p = &a
    fmt. Println (*P)
    //assigns a value to the pointer p
    *p =
    fmt. Println (a)
}
iii. type of pointer

The normal type, the variable is the value, also known as the value type
Gets the address of the variable, using the ampersand,
Pointer type, the variable is stored in an address, the address is the real value
Gets the value pointed to by the pointer type, using the *, for example: Var *p int, and *p to get the P-pointing value

The following code example is used to understand:

Package main

Import "FMT"

func Main () {
    var a int = ten
    fmt. Println (&a)

    var p *int
    p = &a
    fmt. Println (*p)
    *p =
    fmt. Println (a)
}
IV. Process Control

If Else branch is judged

Common format types are as follows:
If condition {
}

If condition {
}else{
}

If condition {
}else if condition {
}else{
}

Switch case

Syntax format:

Switch var {case
    var1: Case
    var2: Case
    VAR3:
    default:

}

If satisfied var1 want to penetrate the next need to add Fallthrough

Examples are as follows:

Package main

Import "FMT"

func Main () {
    var a int = 0

    Switch A {case
    0:
        fmt. Println ("a equals 0")
        Fallthrough case
    1:
        FMT. Println ("a equals 1")
    default:
        FMT. Println ("A equals the default value")
    }}

If we don't add Fallthrough, we'll print a equals 0, but now we're going to print out the print in Case1.

Also here we need to know that the case can be written after the condition

For statement

Grammar
For initialization variable, conditional judgment, variable modification {
}

A simple example

For i:=0;i<100;i++{
    FMT. Println (i)
}

Several other common ways of writing for loops

For condition {
}

The writing of the Dead circle
for {
}

For Range statement

Through an example to understand:

str: = "Hello World" for
i,v: = Range str{
    fmt. Printf ("index[%d] val[%c] len[%d]\n", I,v,len ([]byte (String (v)))
}

One problem to note here is that range str returns two values, one is the subscript of the string, and the other is a single character in the string.

Goto and Label Statements

Package main

Import "FMT"

func Main () {
    label1:for i:=0;i<5;i++{for
        j:=0;j<5;j++{
            if j = = 4{
                continue LABEL1
            }
            Fmt. Printf ("I is:%d and J is:%d\n", I,j)
        }
    }

In the code we added a LABEL1 after the continue so that when the loop matches to J equals 4, it jumps out of the loop, back to the loop of the most outward I, and if there is no LABEL1, it jumps out of the J's Loop and executes J + + into the next loop of J.

We then look at the use of Goto, but we don't recommend Goto in the code

Package main

Import "FMT"

func Main () {
    I: =0
    HEAR:
    fmt. Println (i)
    i++
    if i = = 5{return
    }
    goto HEAR
}

Break and Continue
Sentence explanation: Break is the end of the whole cycle, continue is to terminate the cycle of Five, function detailed

Declaration syntax

Func function name (argument list) [(return list)]{
}

Some common examples of the wording

Func Add () {

}

Func Add (a int,b int) {
}

Func Add (a int,b int) int {

}

Func Add (a int,b int) (int,int) {

}

Func Add (a,b int) (int,int) {

}

The feature of the GO function does not support overloading, a package can not contain the same name function function is a first-class citizen, function is also a type, a function can be assigned to variable anonymous function multiple return value

Several other concepts may be good to understand, we are here mainly to say that the second, function is also a type, a function can be assigned to variables

Use the example below to demonstrate

Package main

Import "FMT"

type Add_func func (int,int) int


func Add (a,b int) int {return
    a+b
}

Func operator (op add_func,a int,b int) int {return
    op (a,b)
}
func main () {
    c: = Add
    fmt. Println (c)
    sum: = operator (c,100,200)
    FMT. Println (sum)
}

Here, a add_func type is customized by type

function parameter Pass mode

Here there are two main ways: value delivery, reference delivery

Whether it's a value pass or a reference pass, passed to the function is a copy of the variable, but the value is passed the copy of the value, reference delivery is the copy of the address, in general, the address copy is more efficient, and the value copy depends on the size of the copied object, the larger the object, the lower the performance

Name of the return value

The following example is used to understand:

Func add_sum (a,b int) (c int) {
    c = a + b
    return 
}

An underscore identifier that is used to ignore the return value

Variable parameters

Represents 0 or more parameters
FUCN Add (Arg ... int) int{
}

Represents 1 or more parameters
Func Add (a int,arg ... int) int {
}
Where Arg is a slice, we can get parameters by Arg[index]
The number of parameters can be judged by Len (ARG)

Defer use

Executes the defer statement when the function returns, so it can be used to do resource cleanup

Multiple defer statements, followed by advanced kitchen style

Defer the variable in the statement, which is determined when the defer declaration is made

The following example is used to understand:

Related Article

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.