create first Golang project

该博客介绍了如何在Go环境下创建一个新的项目‘test0_math’,并在此项目中建立一个名为‘mmath_lib’的模块,实现加减乘除等基本数学运算。通过编写`calculate.go`文件定义函数,然后在`test0.go`中调用这些函数,最后编译并运行项目,输出了计算结果。
摘要由CSDN通过智能技术生成

Reference: https://zhuanlan.zhihu.com/p/109828249

1, create new project "test0_math"

$ cd ~/project

$ mkdir test0_math

2, create new module for project "test0_math"

$ cd test0_math

$ mkdir mmath_lib

$ go mod init test0_math

3, add function for module "mmath_lib"

$ cd mmath_lib

$ touch calculate.go

4, add following lines in function mmath_lib/calculate.go

package mmath_lib

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

func Plus(a, b int) int {
	return (a - b)
}

func Multiple(a, b int) int {
	return (a * b)
}

func Devide(a, b int) (int, int){
	return a/b, a%b
}

5, create main function for project "test0_math"

$ cd ..

$ touch test0.go

6, add following lines in test0.go

package main

import (
	"fmt"
	"test0_math/mmath_lib"
)

func main() {
	fmt.Println("This is test0_math")
	fmt.Println(mmath_lib.Add(1,2))
}

7, compile main function for project "test0_math"

$ go build test0.go

you should find new binary file named "test0"

$ ./test0

you shoud get output:

     This is test0_math
     3

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值