37

与go邂逅(一)——go环境搭建和Helloworld(开发工具:GOLAND)

 5 years ago
source link: https://studygolang.com/articles/20005?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

环境搭建

学习语言,搭建环境必不可少,虽然网上教程也很多,但是我也记录下我的mac上面环境配置

  1. 从网上下载安装文件,下载地址: https://golang.google.cn/dl/
  2. mac 有自带的pkg文件,但是我选择的是go1.12.4.linux-amd64.tar.gz
  3. 下载下来解压即可,会有个命名为go的文件夹
  4. 配置环境变量,修改/etc/profile( 全局的 )或者.bash_profile( 私有的 )即可,配置完执行下source,我的配置如下:
export GOROOT=$HOME/go_dev/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/code/personal/code/go_project

配置完,执行go version查看安装是否成功

开发工具

没有做过比较,只是之前写java的时候用的idea,所以用了goland,它们都是一家公司的,挺好用

创建项目

新建一个项目,配置好路径 bVbrPCN?w=1558&h=964

如图所以创建三个目录 bVbrPCX?w=806&h=1388

创建以.go为结尾的文件,命名随意 bVbrPC6?w=2660&h=936

写一个hello world 执行下:

package main

import "fmt"

func main(){
    fmt.Println("Hello World")
}

写到这里,其实go语言环境就跑通了,其实我们在写java项目的时候,都会有单元测试类,其实go当中也有,只要遵循一定的写法就可以了,我们首先建一个目录:

bVbrPC9?w=2312&h=1046

文件名称需要以_test结尾,代码如下:

package test

import "testing"

//方法名以Test开头,加入参数,*testing.T t
func TestPrint(t *testing.T) {
    t.Log("test hello world")
}

更多文章可以关注公众号:stonezplxjj

bVbrGAt?w=344&h=344


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK