0%

Golang Basic 8 - Pointer

Pointer

  • A pointer holds the memory address of a value.

Declear

  • Pointers in Golang are very similar to pointers in c/c .
    [code lang=golang]
    // declear a pointer
    var pointerName *dataType
    
    //get a pointer's value
    *pointerName
    
    //get a variable's physical address
    &variableName
    [/code]

    Sample

    root@go:l8/ # cat l8s1.go
    package main
    import (
        "fmt"
    )
    
    func swap(val1 *int, val2 *int){
        *val1, *val2 = *val2, *val1
    
    }
    
    func main(){
        var var1, var2 int
        var1 = 3
        var2 = 6
        fmt.Println(var1, var2)
        swap(&var1, &var2)
        fmt.Println(var1, var2)
    
    }
    root@go:l8/ # go run l8s1.go
    3 6
    6 3
    

Golang Basic 7 - Function

Function

Function Declear

  • A function can take zero or more arguments.
  • Notice that the type comes after the variable name. golang func functionName(input_var1 DataType, input_var2 DataType) retDataType{ // some code block here. return retData }

Call a function

functionName(input_var1, input_var2)

Sample

root@go:/develop/go/l7# cat l7s1.go
package main
import (
    "fmt"
)

func add(x int, y int) int{
    return x   y

}

func main(){
    fmt.Println(add(1, 2))

}
root@go:/develop/go/l7# go run l7s1.go
3

Same Parameters' Type

  • If all the parameters have the same data type, we can omit the type from all but the last.
func add(x, y int) int{
    return x   y
}

Sample

root@go:l7/ # cat l7s2.go
package main
import (
    "fmt"
)

func add(x , y int) int{
    return x   y

}

func main(){
    fmt.Println(add(1, 2))

}
root@go:l7/ # go run l7s2.go
3

Multi Return Values

func functionName(input_var1 DataType, input_var2 DataType) retDataType1, retDataType2{
    // some code block here.
    return retData1, retData2
}

Sample

root@go:l7/ # cat l7s3.go
package main
import (
    "fmt"
)

func swap(x , y int) (int, int){
    return y, x

}

func main(){
    var var1, var2 int = swap(1, 2)
    fmt.Println(var1, var2)

}
root@go:l7/ # go run l7s3.go
2 1

Named Return Values

func functionName(input_var1 DataType, input_var2 DataType) variableName1 DataType, variableName2 DataType{
    // some code block here.
    var variableNamel DataType = ...
    var variableName2 DataType = ...
    return
}

Sample

root@go:l7/ # cat l7s4.go
package main
import (
    "fmt"
)

func getAnswer(x , y int) (add int, div float64){
    add = x   y
    div = float64(x) / float64(y)
    return

}

func main(){
    var var1 int
    var var2 float64
    var1, var2 = getAnswer(5, 2)
    fmt.Println(var1, var2)

}
root@go:l7/ # go run l7s4.go
7 2.5

Golang Basic 4 - Basic Operators

Arithmetic Operators

Operators

</colgroup>


</tr>
</thead>


</tr>


</tr>


</tr>


</tr>


</tr>
</tbody>
</table>

Sample

usr@host:go/ # cat l4s1.go
package main
import (
    "fmt"
)

func main(){
    var var1 int
    var var2 int

    var1 = 3
    var2 = 2

    fmt.Printf("%d   %d = %d\n", var1, var2 , var1   var2)
    fmt.Printf("%d - %d = %d\n", var1, var2 , var1 - var2)
    fmt.Printf("%d * %d = %d\n", var1, var2 , var1 * var2)
    fmt.Printf("%d / %d = %d\n", var1, var2 , var1 / var2)
    fmt.Printf("%d %% %d = %d\n", var1, var2 , var1 % var2)

}
usr@host:go/ # go run l4s1.go
3   2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
3 % 2 = 1

Relational Operators

Operators

Operator Sense Description Sample Result
Addition The ‘ ’ operator adds two operands. 3 2 5
- Subtraction The ‘-‘ operator subtracts two operands. 3 - 2 1
* Multiplication The ‘*’ operator multiplies two operands. 3 * 2 6
/ Division The ‘/’ operator divides the first operand by the second. 3 / 2 1
% Modulus The ‘%’ operator returns the remainder when first operand is divided by the second. 3 % 2 1
Operator Sense Description Sample Result
== Equal To Operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. 1 == 3 false
!= Not Equal To Operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. 1 != 3 true
> Greater Than Operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. 1 > 3 false
< Less Than Operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. 1 < 3 true
>= Greater Than Equal To Operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. 1 >= 3 false
<= Less Than Equal To Operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. 1 <= 3 true

Sample

usr@host:go/ # cat l4s2.go
package main
import (
    "fmt"
)

func main(){
    var var1 int
    var var2 int

    var1 = 1
    var2 = 3

    fmt.Printf("%t\n", var1 == var2)
    fmt.Printf("%t\n", var1 != var2)
    fmt.Printf("%t\n", var1 > var2)
    fmt.Printf("%t\n", var1 < var2)
    fmt.Printf("%t\n", var1 >= var2)
    fmt.Printf("%t\n", var1 <= var2)
}
usr@host:go/ # go run l4s2.go
false
true
false
true
false
true

Logical Operators

Operators

Operator Sense Description Sample Result
&& Logical AND The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. true && false false
|| Logical OR The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. true || false true
! Logical NOT The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. !true false

Sample

usr@host:go/ # cat l4s3.go
package main
import (
    "fmt"
)

func main(){
    var flag1 bool = true
    var flag2 bool = false

    fmt.Printf("%t\n", flag1 && flag2)
    fmt.Printf("%t\n", flag1 || flag2)
    fmt.Printf("%t\n", !flag1)
}
usr@host:go/ # go run l4s3.go
false
true
false

Bitwise Operators

Operators

Operator Sense Description Sample Result
& bitwise AND Takes two numbers as operands and does AND on every bit of two numbers. 1 & 2 0
| bitwise OR Takes two numbers as operands and does OR on every bit of two numbers. 1 | 2 3
^ bitwise XOR Takes two numbers as operands and does XOR on every bit of two numbers. 1 ^ 2 3
<< left shift Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. 1 << 2 4
>> right shift Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. 1 >> 2 0
&^ AND NOT This is a bit clear operator. 1 &^ 2 1

Sample

usr@host:go/ # cat l4s4.go
package main
import (
    "fmt"
)

func main(){
    var var1 int = 1
    var var2 int = 2

    fmt.Printf("%d = %04b\n", var1, var1)
    fmt.Printf("%d = %04b\n", var2, var2)

    fmt.Printf("%d : %04b\n", var1 & var2, var1 & var2)
    fmt.Printf("%d : %04b\n", var1 | var2, var1 | var2)
    fmt.Printf("%d : %04b\n", var1 ^ var2, var1 ^ var2)
    fmt.Printf("%d : %04b\n", var1 << var2, var1 << var2)
    fmt.Printf("%d : %04b\n", var1 >> var2, var1 >> var2)
    fmt.Printf("%d : %04b\n", var1 &^ var2, var1 &^ var2)
}
usr@host:go/ # go run l4s4.go
1 = 0001
2 = 0010
0 : 0000
3 : 0011
3 : 0011
4 : 0100
0 : 0000
1 : 0001

Assignment Operators

Operators

Operator Sense Description Sample Result
= Simple Assignment This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. var1 = 1 1
= Add Assignment This operator is combination of ‘ ’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. 1 = 1 2
-= Subtract Assignment This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left. 2 -= 1 1
*= Multiply Assignment This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. 1 *= 2 2
/= Division Assignment This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 2 /= 2 1
%= Modulus Assignment This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 1 %= 2 1
&= Bitwise AND Assignment This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left. 1 &= 2 0
|= Bitwise Inclusive OR This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 0 |= 2 2
^= Bitwise Exclusive OR This operator is combination of ‘ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 2 ^= 2 0
  • You can use ' ', '--', after the variable name, to quickly 1/-1;

Sample

usr@host:go/ # cat l4s5.go
package main
import (
    "fmt"
)

func main(){
    var var1 int
    fmt.Printf("%d\n", var1)

    var1 = 1
    fmt.Printf("%d\n", var1)

    var1  = 1
    fmt.Printf("%d\n", var1)

    var1 -= 1
    fmt.Printf("%d\n", var1)

    var1 *= 2
    fmt.Printf("%d\n", var1)

    var1 /= 2
    fmt.Printf("%d\n", var1)

    var1 %= 2
    fmt.Printf("%d\n", var1)

    var1 &= 2
    fmt.Printf("%d\n", var1)

    var1 |= 2
    fmt.Printf("%d\n", var1)

    var1 ^= 2
    fmt.Printf("%d\n", var1)
}
usr@host:go/ # go run l4s5.go
0
1
2
1
2
1
1
0
2
0

Misc Operators

Operators

Operator Sense Description
& Get Address This operator returns the address of the variable.
* Get Pointer's Value This operator provides pointer to a variable.
<- Receive a value from the channel The name of this operator is receive. It is used to receive a value from the channel.

Sample

usr@host:go/ # cat l4s6.go
package main
import (
    "fmt"
)

func main(){
    var var1 int = 0

    fmt.Printf("value : %d\n", *&var1)
    fmt.Printf("physical : %p\n", &var1)
}
usr@host:go/ # go run l4s6.go
value : 0
physical : 0xc0000180c0

Golang Basic 6 - Basic for loop

for loop


  • Go has only one looping construct, the for loop.

  • The basic for loop has three components separated by semicolons:

    1. the init statement: executed before the first iteration.
    2. the condition expression: evaluated before every iteration.
    3. the post statement: executed at the end of every iteration.

    </li>
  • The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.

  • The loop will stop iterating once the boolean condition evaluates to false.

  • Note: Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the for statement and the braces { } are always required.

  • </ul>

    Basic usage 1

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
    }
    

    Sample

    root@go:l6/ # cat l6s1.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i <= 3{
            fmt.Println(i)
            i
    
        }
    }
    root@go:l6/ # go run l6s1.go
    0
    1
    2
    3
    

    Basic usage 2

    Statement

    for initial; boolVal; repeat{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
    }
    

    Sample

    root@go:l6/ # cat l6s2.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i   {
            fmt.Println(i)
    
        }
    
    }
    root@go:l6/ # go run l6s2.go
    1
    2
    3
    

    Break

    • You can use break statement to break current loop.

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
        if flag {
            // if flag is true, break the for loop.
            break
        }
    }
    

    Sample

    root@go:l6/ # cat l6s3.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i  {
            fmt.Println(i)
            if i == 2{
                break
            }
    
        }
    
    }
    root@go:l6/ # go run l6s3.go
    1
    2
    

    Continue

    • You can use continue, to pass current loop, continue running next loop.

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
        if flag {
            // if flag is true, break the for loop.
            continue
        }
    
        /*
            if the flag is effact, you can not see the result of statements here.
        */
    }
    

    Sample

    root@go:l6/ # cat l6s4.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i  {
            if i == 2{
                continue
    
            }
    
            fmt.Println(i)
    
        }
    
    }
    root@go:l6/ # go run l6s4.go
    1
    3
    

Golang Basic 5 - If statement

if

if statement

if boolVal{
    // if boolVal is true, run this code block.
}

Sample

root@go:l5/ # cat l5s1.go
package main
import (
    "fmt"
)

func main(){
    var flag bool = true

    if flag {
        fmt.Printf("%t\n", flag)
    }
}
root@go:l5/ # go run l5s1.go
true

if... else...

if... else... statement

if boolVal{
    // if boolVal is true, run this code block.
}else{
    //if boolVal is false, run this code block.
}

Sample

root@go:l5/ # cat l5s2.go
package main
import (
    "fmt"
)

func main(){
    var flag bool = false

    if flag {
        fmt.Printf("%t\n", flag)

    }else{
        fmt.Printf("%t\n", flag)

    }
}
root@go:l5/ # go run l5s2.go
false

if... else if... else...

if... else if... else... statement

if boolVal1{
    // if boolVal1 is true, run this code block.
}else if boolVal2{
    // if boolVal2 is true, run this code block.
}else{
    //if boolVal1 and boolVal2 is false, run this code block.
}

Sample

root@go:l5/ # cat l5s3.go
package main
import (
    "fmt"
)

func main(){
    var flag1 bool = false
    var flag2 bool = true

    if flag1 {
        fmt.Printf("flag 1\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }else if flag2 {
        fmt.Printf("flag 2\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }else{
        fmt.Printf("else\n")
        fmt.Printf("%t\n", flag1)
        fmt.Printf("%t\n", flag2)

    }
}
root@go:l5/ # go run l5s3.go
flag 2
false
true

Golang Basic 3 - Basic I/O functions

About fmt package

  • Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler.

Import package

import "fmt"

Functions in fmt

  • You can get more information from https://golang.org/pkg/fmt/#pkg-index. golang func Errorf(format string, a ...interface{}) error func Fprint(w io.Writer, a ...interface{}) (n int, err error) func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Fprintln(w io.Writer, a ...interface{}) (n int, err error) func Fscan(r io.Reader, a ...interface{}) (n int, err error) func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error) func Fscanln(r io.Reader, a ...interface{}) (n int, err error) func Print(a ...interface{}) (n int, err error) func Printf(format string, a ...interface{}) (n int, err error) func Println(a ...interface{}) (n int, err error) func Scan(a ...interface{}) (n int, err error) func Scanf(format string, a ...interface{}) (n int, err error) func Scanln(a ...interface{}) (n int, err error) func Sprint(a ...interface{}) string func Sprintf(format string, a ...interface{}) string func Sprintln(a ...interface{}) string func Sscan(str string, a ...interface{}) (n int, err error) func Sscanf(str string, format string, a ...interface{}) (n int, err error) func Sscanln(str string, a ...interface{}) (n int, err error)

Format string

General

Format Sense
%v The value in a default format when printing structs, the plus flag (% v) adds field names.
%#v A Go-syntax representation of the value.
%T A Go-syntax representation of the type of the value.
%% A literal percent sign; consumes no value.

Boolean

Format Sense
%t The word true or false.

Integer

Format Sense
%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%O base 8 with 0o prefi
%q a single-quoted character literal safely escaped with Go syntax
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U 1234; same as "U %04X"

Floating-point and complex constituents

Format Sense
%b decimalless scientific notation with exponent a power of two, in the manner of strconv.FormatFloat with the 'b' format
%e scientific notation
%E scientific notation
%f decimal point but no exponent
%F synonym for %f
%g %e for large exponents, %f otherwise
%G %E for large exponents, %F otherwise
%x hexadecimal notation (with decimal power of two exponent)
%X upper-case hexadecimal notation

String and slice of bytes

Format Sense
%s the uninterpreted bytes of the string or slice
%q a double-quoted string safely escaped with Go syntax
%x base 16, lower-case, two characters per byte
%X base 16, upper-case, two characters per byte

Slice

Format Sense
%p address of 0th element in base 16 notation, with leading 0x

Pointer

|Format|Sense| |:-:|:-:| |%p|base 16 notation, with leading 0x| - The %b, %d, %o, %x and %X verbs also work with pointers, formatting the value exactly as if it were an integer.

Basic input function

Some functions could get input from console.

func Scan(a ...interface{}) (n int, err error)
func Scanln(a ...interface{}) (n int, err error)
  • If you need set string format, you can use Scanf().
  • Scanln() will add a end of line symbol at the end of the line.

Basic output function

Some functions could print on the console.

func Print(a ...interface{}) (n int, err error)
func Printf(format string, a ...interface{}) (n int, err error)
func Println(a ...interface{}) (n int, err error)
  • If you need set string format, you can use Printf().
  • Println() will add a end of line symbol at the end of the line.

Sample

usr@host:go/ # cat l3s1.go
package main
import (
    "fmt"
)

func main(){
    var var1 int
    var var2 int

    fmt.Print("Please input two values (split with blank) : ")
    fmt.Scanln(&var1, &var2)
    fmt.Printf("%d   %d = %d\n", var1, var2, var1   var2)
}
usr@host:go/ # go run l3s1.go
Please input two values (split with blank) : 3 6
3   6 = 9

Golang Basic 2 - Basic Data Types && Variable

Basic Data Types

  • int, eg. 0, 1, 2, 3.
  • float32, float64, eg. 3.14159.
  • string, eg. "Hello World".
  • bool, eg. true, false.
  • rune, eg. 'a', 'b'. (char, saved as int)

Variable

Declear variable

var VariableName DataType

Set value

VariableName = Value

Sample

usr@host:go/ # cat l2s1.go
package main

import (
    "fmt"
)

func main(){
    // declear variables
    var int_val int         // declear an int variable
    var float_val float64   // declear a float variable
    var string_val string   // declear a string variable
    var bool_val bool       // declear a bool variable
    var rune_val rune       // declear a rune variable

    // set value for variables
    int_val = 3
    float_val = 1.234
    string_val = "Hello"
    bool_val = true
    rune_val = 'A'

    // print them
    fmt.Println(int_val)
    fmt.Println(float_val)
    fmt.Println(string_val)
    fmt.Println(bool_val)
    fmt.Println(rune_val)

}
usr@host:go/ # go build l2s1.go
usr@host:go/ # ./l2s1
3
1.234
Hello
true
65

Declear && Set value at the same time

    var VariableName DataType = Value

Sample

usr@host:go/ # cat l2s2.go
package main

import (
    "fmt"
)

func main(){
    // declear and set variables
    var int_val int = 3
    var float_val float64 = 1.234
    var string_val string = "Hello"
    var bool_val bool = true
    var rune_val rune = 'A'

    // print them
    fmt.Println(int_val)
    fmt.Println(float_val)
    fmt.Println(string_val)
    fmt.Println(bool_val)
    fmt.Println(rune_val)

}
usr@host:go/ # go build l2s2.go
usr@host:go/ # ./l2s2
3
1.234
Hello
true
65

Change values in variable

usr@host:go/ # cat l2s3.go
package main

import (
    "fmt"
)

func main(){
    // declear and set variable
    var int_val int = 3

    // print them
    fmt.Println(int_val)

    // set new value for variable
    int_val = 5

    // print them
    fmt.Println(int_val)

}
usr@host:go/ # go build l2s3.go
usr@host:go/ # ./l2s3
3
5

Some errors

usr@host:go/ # cat l2s4.go
package main

import (
    "fmt"
)

func main(){
    // declear and set variable
    var int_val int = 3

    // print them
    fmt.Println(int_val)

    // set new value for variable
    int_val = "abc"

    // print them
    fmt.Println(int_val)

}
usr@host:go/ # go build l2s4.go
# command-line-arguments
./l2s4.go:15:15: cannot use "abc" (type string) as type int in assignment
  • The values we set for the variable must match the data type.

Golang Basic 1 - Install && Hello World

What is Golang

  • Official website: https://golang.org/
  • Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

How to install

Comments


  • 3 way to write a comments in go program.

  • // this is a single line comment
    
  • /*
    this is a
        multi line
            comment
    */
    

Hello World

usr@host:go/ # cat hello.go
package main    // if you want to run this program
import "fmt"    // import some build in packages, fmt is a packag e for print something on the screen
func main(){    // the main function, the entry of the program
    fmt.Println("Hello world.!")    // call fmt.Println to print something on the screen
}
usr@host:go/ # go build hello.go    # build the program
usr@host:go/ # ./hello  # run it
Hello world.!

1st time I try to install word2vec

root@dev:/# pip install word2vec
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting word2vec
 Downloading https://mirrors.aliyun.com/pypi/packages/ce/51/5e2782b204015c8aef0ac830297c2f2735143ec90f592b9b3b909bb89757/word2vec-0.10.2.tar.gz (60kB)
 100% |████████████████████████████████| 61kB 1.1MB/s
 Complete output from command python setup.py egg_info:
 Traceback (most recent call last):
 File "", line 1, in
 File "/tmp/pip-install-rj7udqcw/word2vec/setup.py", line 4, in
 from Cython.Build import cythonize
 ModuleNotFoundError: No module named 'Cython'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-rj7udqcw/word2vec/

resolve it

root@dev:/# pip install Cython
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting Cython
 Downloading https://mirrors.aliyun.com/pypi/packages/e1/fd/711507fa396064bf716493861d6955af45369d2c470548e34af20b79d4d4/Cython-0.29.6-cp36-cp36m-manylinux1_x86_64.whl (2.1MB)
 100% |████████████████████████████████| 2.1MB 58.3MB/s
Installing collected packages: Cython
Successfully installed Cython-0.29.6
root@dev:/# pip install word2vec
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting word2vec
 Downloading https://mirrors.aliyun.com/pypi/packages/ce/51/5e2782b204015c8aef0ac830297c2f2735143ec90f592b9b3b909bb89757/word2vec-0.10.2.tar.gz (60kB)
 100% |████████████████████████████████| 61kB 1.4MB/s
Requirement already satisfied: cython in /usr/local/lib/python3.6/dist-packages (from word2vec) (0.29.6)
Requirement already satisfied: numpy>=1.9.2 in /usr/local/lib/python3.6/dist-packages (from word2vec) (1.15.4)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.6/dist-packages (from word2vec) (0.20.1)
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from word2vec) (1.1.0)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from word2vec) (1.11.0)
Building wheels for collected packages: word2vec
 Building wheel for word2vec (setup.py) ... done
 Stored in directory: /root/.cache/pip/wheels/6c/41/28/8a47f03d8b1387e2360e13f9719847eb545d0daa5f65d44ef3
Successfully built word2vec
Installing collected packages: word2vec
Successfully installed word2vec-0.10.2

System Env

[root@ansible ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@ansible ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
[root@ansible ~]# ansible --version
ansible 2.7.7
 config file = None
 configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
 ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
 executable location = /usr/local/bin/ansible
 python version = 3.6.6 (default, Jan 26 2019, 16:53:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Generate Ansible Server's public key

[root@ansible ~]# ssh-keygen

Deploy Ansible Server's public key

  • Deploy public key to s1
    [root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@s1
    

Add Hosts for Ansible

[root@ansible ~]# mkdir /etc/ansible
[root@ansible ~]# vim /etc/ansible/hosts
[root@ansible ~]# cat /etc/ansible/hosts
[servers]
s1

Run some test command

[root@ansible ~]# ansible s1 -m ping
s1 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}