我的Go+语言初体验——Go+学习笔记

ndzjx 2021-12-16 20:10:54

参与活动:“我的Go+语言初体验”征文大赛 | 70000奖金池、社区专属贡献者荣誉头衔和限量周边礼品等你来瓜分!-CSDN社区 

学习一下:

foo/foo.gop

package foo 

func ReverseMap(m map[string]int) map[int]string {
    return {v: k for k, v <- m}
}

main.gop

package main

import (
	"fmt"
	"math/big"
	"strconv"
	"strings"
	"test/foo"
)

func plot(fn func(x float64) float64) {
	println "aaaa", fn(5.0)
}

func plot2(fn func(x float64) (float64, float64)) {
	println fn(10.0)
}

func plot3(fn func(x float64, y float64) (float64, float64)) {
	println fn(10.0, 2)
}

type Config struct {
	Dir   string
	Level int
}

func foo2(conf *Config) {
	println conf.Dir, conf.Level
}

func foo2_1(conf Config) {
	println conf.Dir, conf.Level
}

func foo3() *Config {
	return {Dir: "/foo2", Level: 2}
}

func foo3_1() Config {
	return {Dir: "/foo2", Level: 2}
}

type student struct {
	name  string
	score int
}

type Foo struct {
	name  string
	score int
}

// Gop_Enum(proc func(val ValType)) or:
// Gop_Enum(proc func(key KeyType, val ValType))
// 有这个 才允许range
func (p *Foo) Gop_Enum(proc func(key int, val string)) {
	println "--", p.name
}

type MyBigInt struct {
	*big.Int
}

func Int(v *big.Int) MyBigInt {
	return MyBigInt{v}
}

func (a MyBigInt) + (b MyBigInt) MyBigInt { // binary operator
	return MyBigInt{new(big.Int).Add(a.Int, b.Int)}
}

func (a MyBigInt) += (b MyBigInt) {
	a.Int.Add(a.Int, b.Int)
}

func -(a MyBigInt) MyBigInt { // unary operator
	return MyBigInt{new(big.Int).Neg(a.Int)}
}

func add(x, y string) (int, error) {
	return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
}

func addSafe(x, y string) int {
	return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
}

func main() {
	if true {
		println `add("100", "23"):`, add("100", "23")!

		sum, err := add("10", "abc")
		println `add("10", "abc"):`, sum, err

		println `addSafe("10", "abc"):`, addSafe("10", "abc")
	}
	if false {
		a := Int(1r)
		a += Int(2r)
		println a+Int(3r)
		println -a
	}
	if false {
		foo := &Foo{name: "liming", score: 20}
		for k, v := range foo {
			println k, v
		}
		println "-------"
		for k, v <- foo {
			println k, v
		}
		println "-------"
		println {v: k for k, v <- foo}
	}
	if false {
		for i <- :10 {
			print i, " "
		}
		println

		for i := range :10:2 {
			print i, " "
		}
		println

		for i := range 1:10:3 {
			print i, " "
		}
		println

		for range :10 {
			println "Range expression"
		}
	}
	if false {
		sum := 0
		for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
			sum += x
		}
		println sum
	}
	if false {
		students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]
		unknownScore, ok := {x.score for x <- students, x.name == "Unknown"}
		jasonScore := {x.score for x <- students, x.name == "Jason"}
		println unknownScore, ok // output: 0 false
		println jasonScore       // output: 80

		hasJason := {for x <- students, x.name == "Jason"} // is any student named Jason?
		hasFailed := {for x <- students, x.score < 60}     // is any student failed?
		println hasJason, hasFailed
	}
	if false {
		// 列表推导
		a := [x*x for x <- [1, 3, 5, 7, 11]]
		b := [x*x for x <- [1, 3, 5, 7, 11], x > 3]
		c := [i+v for i, v <- [1, 3, 5, 7, 11], i%2 == 1]
		d := [k+","+s for k, s <- {"Hello": "xsw", "Hi": "Go+"}]
		println a, b, c, d

		arr := [1, 2, 3, 4, 5, 6]
		e := [[a, b] for a <- arr, a < b for b <- arr, b > 2]
		x := {x: i for i, x <- [1, 3, 5, 7, 11]}
		y := {x: i for i, x <- [1, 3, 5, 7, 11], i%2 == 1}
		z := {v: k for k, v <- {1: "Hello", 3: "Hi", 5: "xsw", 7: "Go+"}, k > 3}
		println e, x, y, z
	}
	if false {
		// 类型推断
		foo2 {Dir: "/foo/bar", Level: 1}
		foo2(&Config{Dir: "/foo/bar", Level: 1})
		foo2_1(foo3_1())
		foo2(foo3())
	}
	if false {
		// x => x*x is a closure
		plot x => x * x           // plot(func(x float64) float64 { return x * x })
		plot2 x => (x * x, x + x) // plot2(func(x float64) (float64, float64) { return x * x, x + x })
		println "======="
		plot(x => {
			println "ccc"
			return x * x
		})
		plot2(x => (x, x * x * x))
		plot3((x, y) => (x + y, x * y))
		plot3((x, y) => {
			return x + y, x * y
		})
	}
	if false {
		x := [1, 3.4]       // []float64
		y := [1]            // []int
		z := [1+2i, "xsw"]  // []interface{}
		a := [1, 3.4, 3+4i] // []complex128
		b := [5+6i]         // []complex128
		c := ["xsw", 3]     // []interface{}
		empty := []         // []interface{}

		println x, y, z, a, b, c, empty
	}
	if false {
		x := {"Hello": 1, "xsw": 3.4}   // map[string]float64
		y := {"Hello": 1, "xsw": "Go+"} // map[string]interface{}
		z := {"Hello": 1, "xsw": 3}     // map[string]int
		empty := {}                     // map[string]interface{}
		println x, y, z, empty
		println x["xsw"], y["Hello"], z["xsw"]
	}
	if false {
		println [x*x for x <- 1:6:2] // output: [1 9 25]

		mapData := {"Hi": 1, "Hello": 2, "Go+": 3}
		reversedMap := {v: k for k, v <- mapData}
		println reversedMap // output: map[1:Hi 2:Hello 3:Go+]
	}
	if false {
		var a bigint = 1r << 65 // bigint, large than int64
		var b bigrat = 4/5r     // bigrat
		c := b - 1/3r + 3*1/2r  // bigrat
		println a, b, c

		var x *big.Int = 1r << 65 // (1r << 65) is untyped bigint, and can be assigned to *big.Int
		var y *big.Rat = 4/5r
		println x, y
	}
	if false {
		rmap := foo.ReverseMap(map[string]int{"Hi": 1, "hello": 2})
		fmt.Println(rmap)
	}
	if false {
		x := strings.NewReplacer("?", "!").Replace("Hello, world?")
		fmt.Println("x: ", x)
	}
}

// 编译当前目录所有包

gop build ./...

// 编译生成 可执行人间

gop build 

 

总体的感受:Go+语法相对Go有很大的简化,尤其是循环/闭包/列表推导/错误处理

因为毕竟刚起步,vscode 没有直接可以单步调试的插件。实际上Go+在编译时也是翻译成Go中间语言,再生成的可执行文件,希望以后可以直接单步调试Go+.

 

整体上看,相对Go来说,确实很简洁了,毕竟是国人开发的语言,在工程中使用起来,跟Go差不多,会稍微简洁一些。

 

 

 

 

 

...全文
721 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ndzjx 2021-12-19
  • 打赏
  • 举报
回复

欢迎大家一起学习。

ζ小菜鸡 社区贡献者 2021-12-17
  • 打赏
  • 举报
回复 1

学习到了

1,097

社区成员

发帖
与我相关
我的任务
社区描述
Go+ 官方开发者社区。我们希望向广大的开发者和数据科学家介绍 Go+ 的定位和意义,并邀请更多开发者一起贡献代码、共建 Go+ 生态。 Go+ 官网:https://goplus.org/
其他 企业社区
社区管理员
  • Go+
  • 杨东杰
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

本社区为 Go+ 官方开发者社区。我们希望向广大的开发者和数据科学家介绍 Go+ 的定位和意义,并邀请更多开发者一起贡献代码、共建 Go+ 生态。

Go+ 官网:https://goplus.org/
GitHub地址:https://github.com/goplus/gop

试试用AI创作助手写篇文章吧