zoukankan      html  css  js  c++  java
  • 001_Go hello world

    一、go获取程序参数及指针地址示例

    package main
    import (
    	"fmt"
    	"os"
    )
    
    func main()  {
    	fmt.Println(os.Args);
    
    	if len(os.Args) > 1{
    		fmt.Println("Hi", os.Args[1])
    	}else {
    		fmt.Println("Hello world")
    		os.Exit(3)
    	}
    
    	fmt.Println(*foo())
    }
    
    
    func foo() *string{
    	s := "Hello arun"
    	return &s
    }/*
    Hi ggg
    Hello arun
    */
    

    (1)go run helloworld.go

    (2)go build helloworld.go && ./hellowold

    注意事项:main函数没有参数和返回值

    package main
    import (
    	"fmt"
    	"os"
    )
    
    //func main() int { //func main must have no arguments and no return values
    func main() { //func main must have no arguments and no return values
    	fmt.Println("Hello arun!")
    	//return 1
    	os.Exit(-1)
    }
    

    二、编写测试go测试程序

    1.源码文件以_test结尾: xxx_test.go

    2.测试方法名以Test开头: func TestXXX(t *testing.T) {...}

    package fib
    import (
    	"fmt"
    	"testing"
    )
    
    func TestFibonacciCreation(t *testing.T)  {
    	//t.Log("Log print")
    /*	var a int =1
    	var b = 1
    	var n  int = 5*/
    
    /*	var (
    		a int = 1
    		b  = 1 //自动类型推断
    		n  int = 5
    	)*/
    
    	var a, b , n= 1, 1, 5
    
    	fmt.Print(a)
    	for i :=0;/*短变量声明 := */ i< n; i++{
    		fmt.Print(" ", b)
    		tmp := a
    		a = b
    		b = tmp + b
    	}
    	fmt.Println()
    }/*
    === RUN   TestFibonacciCreation
    1 1 2 3 5 8
    --- PASS: TestFibonacciCreation (0.00s)
    PASS
    */
    

      09:04

  • 相关阅读:
    点赞
    js点击事件,数字累加
    html中hr的各种样式使用
    基于Bootstrap垂直响应的jQuery时间轴特效
    bootstrop日历
    前端经验
    bootstrop登陆页面
    bootstrop设置背景图片自适应屏幕
    建立博客的第一天
    php伪静态--隐藏地址实际路径方法
  • 原文地址:https://www.cnblogs.com/arun-python/p/10474580.html
Copyright © 2011-2022 走看看