zoukankan      html  css  js  c++  java
  • (Go)04.go工作区目录规范及简单例子

    一.规范目录结构

    D:projectsrcgo_devday1example1

    二.设置GOPAH环境变量

    三.hello world

    1.hello world

    1 package main
    2 
    3 import (
    4     "fmt"
    5 )
    6 
    7 func main () {
    8     fmt.Println("Hello World!")
    9 }

    go build go_devday1example1

     

    生成example1.exe

    执行example1.exe

    2.goroute循环

    goroute.go

    1 package main
    2 
    3 import (
    4     "fmt"
    5 )
    6 
    7 func test_goroute(a int) {
    8     fmt.Println(a) 
    9 }

    main.go

     1 package main
     2 
     3 import (
     4     "time"
     5 )
     6 
     7 
     8 func main() {
     9 
    10     for i :=0; i <100; i++ {
    11         go test_goroute(i)
    12     }
    13     time.Sleep(2*time.Second)
    14 }

    3.goroute_example

    代码目录结构

    1 package goroute
    2 
    3 
    4 func Add(a int, b int, c chan int) {
    5     sum := a + b
    6     c <- sum
    7 
    8 } 
    add.go
     1 package main
     2 
     3 import (
     4     "go_dev/day1/goroute_example/goroute"
     5     "fmt"
     6 )
     7 
     8 
     9 func main() {
    10     //var pipe chan int
    11     //pipe = make(chan int, 1) 
    12     
    13     pipe := make(chan int, 1)   //等同于上二行
    14     go goroute.Add(100, 300, pipe)
    15 
    16 
    17     sum := <- pipe
    18     fmt.Println("sum =", sum)
    19 }
    main.go

    构建代码

    D:project>go build -o bin/goroute_example.exe go_dev/day1/goroute_example/main   

    D:projectin>goroute_example.exe
    sum = 400
    

      

  • 相关阅读:
    学习笔记 线程异步请求过程
    学习笔记 urllib
    学习笔记 requests + BeautifulSoup
    python3 kmp 字符串匹配
    python3:实现字符串的全排列(有重复字符)
    python3:实现字符串的全排列(无重复字符)
    python 贝叶斯算法
    knn算法
    python基础5
    python基础4
  • 原文地址:https://www.cnblogs.com/lvcisco/p/10313420.html
Copyright © 2011-2022 走看看