CentOS7安装Go环境简单的tcp服务端和客户端测试

下载最新稳定版本

wget https://studygolang.com/dl/golang/go1.14.3.linux-amd64.tar.gz

tar zxvf go1.14.3.linux-amd64.tar.gz

mv go /home/ 

我放置目录 /home/go 

设置环境变量

vim /etc/profile
export GOROOT=/home/go
export GOPATH=/mnt/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
保存
esc
:wq
还有一种比较简便的方式 (就是输入一字字符向/etc/profile文件尾进行追加)

#一定要>>  不要写成>了,不然就要悲剧了。全部清空了

cp /etc/profile  /etc/profile2    #做一次备份

echo 'export GOROOT=/home/go'>> /etc/profile

echo 'export GOPATH=/mnt/go'>> /etc/profile

echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin'>> /etc/profile
source /etc/profile   #让环境变量生效
[root@VM_0_13_centos bin]# go   #输入go,验证是否安装成功
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]

The commands are:

	bug         start a bug report
	build       compile packages and dependencies
	clean       remove object files and cached files
	doc         show documentation for package or symbol
	env         print Go environment information
	fix         update packages to use new APIs
	fmt         gofmt (reformat) package sources
	generate    generate Go files by processing source
	get         add dependencies to current module and install them
	install     compile and install packages and dependencies
	list        list packages or modules
	mod         module maintenance
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

	buildmode   build modes
	c           calling between Go and C
	cache       build and test caching
	environment environment variables
	filetype    file types
	go.mod      the go.mod file
	gopath      GOPATH environment variable
	gopath-get  legacy GOPATH go get
	goproxy     module proxy protocol
	importpath  import path syntax
	modules     modules, module versions, and more
	module-get  module-aware go get
	module-auth module authentication using go.sum
	module-private module configuration for non-public modules
	packages    package lists and patterns
	testflag    testing flags
	testfunc    testing functions

Use "go help <topic>" for more information about that topic.
[root@VM_0_13_centos bin]# go env   #验证下环境配置
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/mnt/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build057612834=/tmp/go-build -gno-record-gcc-switches"

 

测试环境

创建目录 /mnt/go/src   (创建的工程项目文件地址)

$mkdir helloworld

$cd helloworld

$vim main.go

输入

package main

import "fmt"

func main(){

	var name string
	fmt.Println("请输入你的名字:")
	fmt.Scanln(&name)
	fmt.Println("你好",name)

}

 

[root@VM_0_13_centos helloworld]# go run ./main.go 
请输入你的名字:
中国人
你好 中国人
[root@VM_0_13_centos helloworld]# 

加点料,写一个简单的tcp server

server.go

package main

import (
	"log"
	"net"
	"os"
)

func recvMessage(client net.Conn) error {
	var message []byte
	message = make([]byte, 1024)

	for {
		len, _ := client.Read(message)
		if len > 0 {
			log.Println(message[0:len])
		}
	}

	return nil
}

func main() {
	server, err := net.Listen("tcp", "0.0.0.0:6100")
	if err != nil {
		log.Fatal("start server failed!\n")
		os.Exit(1)
	}
	defer server.Close()

	log.Println("server is running...")
	for {
		client, err := server.Accept()
		if err != nil {
			log.Fatal("Accept error\n")
			continue
		}

		log.Println("the client is connectted...")
		go recvMessage(client)
	}
}
[root@VM_0_13_centos helloworld]# go run server.go 
2020/05/18 23:37:03 server is running...
2020/05/18 23:37:15 the client is connectted...
2020/05/18 23:37:41 [116 101 115 116]           #接收的信息

 

客户端

 

client.go

package main

import (
	"log"
	"net"
	"os"
)

func main() {
	client, err := net.Dial("tcp", "localhost:6100")
	if err != nil {
		log.Fatal("Client is dailing failed!")
		os.Exit(1)
	}

	client.Write([]byte("i am client"))

	client.Close()
}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值