golang第一天

栏目: Go · 发布时间: 7年前

环境搭建

  1. win10环境,.msi软件一直下一步,安装结束,path环境变量自动配置
  2. 测试环境是否搭建成功,使用go version命令
golang第一天

1.png

测试代码

  • hello.go, 包含 go 的基本语法和类型
package  main   //主包

import (
    "fmt"   //打印模块
    "math"  //数据运算模块
)

/**
 * 常量的声明
 * 只能为:布尔型、数字型(整数型、浮点型和复数)和字符串型
 */
const (
    MY_NAME = "JACK"
    MY_AGE int = 19
    MY_FLAG bool = true
)

/**
 * 程序入口 main函数和main包
 */
func main()  {
    /**
     * 声明一个字符串类型变量
     */
    var message string = "hello,go!"
    fmt.Println(message)

    /**
     * 声明变量的两种方式
     */
    var age int =10
    name := "jack"

    /**
     * 占位符格式
     */
    fmt.Printf("%d\n", age)
    fmt.Printf("%s\n", name)

    /**
     * if-else
     */
    if age>10 { //if-else
        fmt.Println("old")
    } else {
        fmt.Println("young")
    }

    /**
     * for循环, while, do-while不存在(for写出)
     * break,continue类似其他语言
     */
    for idx:=0; idx<100; idx++ {    //循环语句,
        fmt.Println("idx=", idx)
    }

    for age<20 {    //while
        fmt.Println("age=", age)
        age++
    }

    num1, num2 := 10, 20
    num1, num2 = swap(num1, num2)
    fmt.Println(num1, num2)

    /**
     * 2-100内的质数
     */
    for idx:=2; idx<=100; idx++ {
        if is_prime(idx) {
            fmt.Printf("%d是质数\n", idx)
        }
    }


    /**
     * switch-case
     */
    var myAge int = 50
    switch myAge {
    case 50, 60:
        fmt.Println("a")
        fallthrough         //强制执行下一条case
    case 40:
        fmt.Println("b")
    case 30:
        fmt.Println("c")
    }
    //output: a
    //fallthrough存在,output: a b

}

/**
 * 判断是否为质数
 * 返回为bool, 布尔类型
 */
func is_prime(num int) bool {
    for idx:=2;idx<=int(math.Sqrt(float64(num))); idx++ {   //暂时这样写
        if num%idx==0 {
            return false
        }
    }
    return true
}

/*
 * 返回多个值
 */
func swap(num1 int, num2 int) (int, int)  {
    return num2, num1
}

//go run hello.go编译且运行程序
//go build hello.go生成hello.exe的可执行文件
//output: hello,go!
  • 打卡时间22:52

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Computers and Intractability

Computers and Intractability

M R Garey、D S Johnson / W. H. Freeman / 1979-4-26 / GBP 53.99

This book's introduction features a humorous story of a man with a line of people behind him, who explains to his boss, "I can't find an efficient algorithm, but neither can all these famous people." ......一起来看看 《Computers and Intractability》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码