当前位置:网站首页>The language of initial
The language of initial
2022-08-02 14:20:00 【Spaghetti Mixed with No. 42 Concrete】
Go语言初始
一、goLanguage introduction and development
# 好多公司:python+go混用,架构师---》前端,运维,测试,Backend various languages
# Go 即Golang,是Google公司2009年11月正式对外公开的一门编程语言--》12年
# real development6,7年时间,很新,Cloud native fire--docker,k8s
# GoThere are some pits----》一直在发展---》泛型
# python 1989年 java 1990年
# Go是静态强类型语言,是区别于解析型语言的编译型语言(静态:类型固定 强类型:不同类型不允许直接运算)
# go最大的好处:跨平台编译---》mac写的代码--》编译完了--》发给你女朋友,直接可以运行,部署简单
# php部署---》php解释器---》模块---》运行起来
# python部署----》解释器--》装模块---》运行起来
# java部署---》字节码文件---》运行在jvm(java虚拟机)---》装jdk--》运行起来
# go部署---》可执行文件----》运行即可
# 跨平台的编译型语言
-python,php,nodejs天然跨平台---》解释器--》run on the interpreter---》The interpreter is cross-platform
-java号称跨平台----》javaBytecode runs on a virtual machine---》Install virtual machines on different platforms
-c,c++ 不支持跨平台,It needs to be compiled into executable files on different platforms and then executed
-go 跨平台编译--》可以在win上编译成linux可执行文件
# 支持面向对象
# 区块链,云原生
-The first open source blockchain codego写的
二、go语言开发环境搭建
# Development needs to be installed
# python解释器 pycharm IDE集成开发工具
# go sdk goland IDE集成开发工具 ---》jetbrains全家桶
# 下载:https://golang.google.cn/dl/ ----》1.17.x--》一路下一步--》Automatically add the installation path to the environment variable
# Type on the command line
go version #Just see the outputok了
# goland和Golang:
三、第一个helloworld
package main // 每个goA file must belong to a package
import "fmt" //导入包fmt
func main() {
// main函数
fmt.Println("hello world")
}
// go 是编译型语言---》先编译---》再执行
// go build s1.go---->s1.exe
// 执行可执行文件 s1.exe
// go run s1.exe 编译并运行(Where did the compilation go?用完就删了)
// go要运行,必须是main包下的main函数---》入口
/*
go build 编译
go run 编译并运行
交叉编译
*/
// win Compiled and executable for different platforms
// Mac
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build filename.go
// Linux
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build filename.go
// go env go的环境变量
GO111MODULE= // The new version is enabled by default,是否开启go module模式
GOPROXY=https://goproxy.cn,direct // 下载go 模块的地址
GOROOT=D:\Program Files\Go // go的安装路径,必须对,If it's not right there will be problems
GOPATH=C:\Users\oldboy\go // 如果使用go path模式,All code must be placed in this folderbin路径下
// 在代码中 import "fmt",Where did you import fromfmt---》就是从go root路径下
//There may be third-party packages,使用go path模式,The import path is fromgo path的binStart counting down the path
四、命名规范
# go文件名的命名规范:Use underscore as much as possible python用下划线
# go的变量命名规范:CamelCase is recommended python用下划线
# Keywords and reserved words cannot be used as variable names
// Go语言中关键字有25个;关键字不能用于自定义名字,只能在特定语法结构中使用
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
// go语言中有37个保留字,主要对应内建的常量、类型和函数
内建常量: true false iota nil
内建类型: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error
内建函数: make len cap new append copy close delete
complex real imag
panic recover
//goto python中没有 javaas a reserved keyword,但是没有作用 go,c都有goto,
五、变量
package main
import "fmt"
func main() {
//1 The full definition of the variable
//var关键字 变量名 变量类型 = 变量值
//var name string ="lqz" // 变量定义了必须使用,如果不使用就报错
//fmt.Println(name)
//2 类型推导 Type is not written,It's not that there are no types,推导出来的
//var name ="lqz"
//fmt.Println(name)
//3 简略声明 :跟=是一家 Type is also fixed
//name := "lqz"
//fmt.Println(name)
//4 goOnce the variable type of the language is determined,不能更改
//age := 19 // 就是int类型
//age="lqz" // 不能这么用
// 5 变量要先定义,在使用,不能重复定义
//var age int // 定义
//age=99 //赋初值
//fmt.Println(age) // 使用
//var age=99
//age:=99
//fmt.Println(age)
// 6 同时定义多个变量
//var name ="lqz"
//var age =99
//var hobby="篮球"
//var name,age,hobby string,int,string="lqz",99,"篮球" //这个不行
var (
name string="lqz"
age=99
hobby string="篮球"
)
//var name,age,hobby="lqz",99,"篮球"
// name,age,hobby :="lqz",99,"篮球"
//var width, height int = 100, 50 // 可以
fmt.Println(name)
fmt.Println(age)
fmt.Println(hobby)
}
六、Python和Golanguage mixing
1 、动态链接库(dll,so文件)
Linux下的动态库以.so 结尾
Windows下的动态库以.dll结尾
2 、GoLanguages are packaged into dynamic link library files(dll,so)
2.1 windows下
golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW.
windows 64 Bit system should be downloaded at : https://sourceforge.net/projects/mingw-w64/
下载后运行 mingw-w64-install.exe,完成 MingGW 的安装
2.2 mac,Linux下
自带gcc,无需任何安装
2.3 编写Go代码
package main
import "C" //必须引入C库
import "fmt"
//Add the commented code below,表示导出,可以被python调用
//export PrintDll
func PrintDll() {
fmt.Println("我来自dll")
}
//export Sum
func Sum(a int, b int) int {
return a + b
}
func main() {
//必须加一个main函数,作为CGO编译的入口,No specific implementation code
}
注意:
1 注释 //export PrintBye 和 //export Sum 在编译 动态库(dll ,so)的时候是必须的,说明了 动态库 需要输出的函数
2 main函数必须写,没有执行代码,作为CGO编译的入口
3 必须引入C库(import “C”
2.4 编译成so库
go build -buildmode=c-shared -o s1.so s1.go
2.4 编译成dll库
go build -buildmode=c-shared -o s1.so s1.go
注意:
1 -o表示输出,s1.so 表示编译成so文件的名字,s1.goIndicates which to compilego文件
2 “-s -w” Options are used to reduce the size of the generated dynamic link library,-s 是压缩,-w is to remove debug information
go build -ldflags “-s -w” -o main.dll -buildmode=c-shared s1.go
3 编译模式buildmode
模式 | 说明 |
archive | 编译成二进制文件.Usually static library files. xx.a |
c-archive | 编译成C归档文件.CA callable static library.xx.a.Note that it is necessary to compile into such filesimport C 并且要 Externally called functions are to be used “//export 函数名” way to comment above the function.Otherwise the function will not be exported by default. |
c-shared | 编译成C共享库.同样需要 import “C” and comment above the function // export xxx |
default | 对于有mainThe package is compiled directly into an executable.没有main包的,编译成.a文件 |
编译成window可执行程序 | |
plugin | 将mainThe package is compiled together with the dependent packagesgo plugin.非main包忽略.【类似Cshared library or static library.Plug-in development and use |
3、使用python语言来调用
from ctypes import cdll
lib = cdll.LoadLibrary('./s1.so')
# 调用go语言的Sum
result = lib.Sum(100, 200)
print(result)
# 调用go语言的PrintDll
lib.PrintDll()
边栏推荐
猜你喜欢
Flask项目的完整创建 七牛云与容联云
redis分布式锁和看门狗的实现
理解TCP长连接(Keepalive)
[ROS] Introduction to common tools in ROS (to be continued)
mysql的case when如何用
如何选择正规的期货交易平台开户?
你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
关于市场后市的发展预测? 2021-05-23
随机推荐
Flask-SQLAlchemy
第十二单元 关联序列化处理
8576 Basic operations of sequential linear tables
瑞吉外卖笔记——第08讲读写分离
监管再次重拳出击,后市如何?2021-05-22
Sentinel源码(四)(滑动窗口流量统计)
[ROS] The difference between roscd and cd
如何解决1045无法登录mysql服务器
What is the difference between web testing and app testing?
chapter6可视化(不想看版)
Raft协议图解,缺陷以及优化
跑yolov5又出啥问题了(1)p,r,map全部为0
LayoutParams的详解
How does Apache, the world's largest open source foundation, work?
【ROS】工控机的软件包不编译
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
drf视图组件
8581 线性链表逆置
线代:已知一个特征向量快速求另外两个与之正交的特征向量
第四单元 路由层