All Projects → ymzuiku → hit

ymzuiku / hit

Licence: other
在Golang中使用三木运算, 测试覆盖率100%, 请放心使用 / Use ternary operator in Golang

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to hit

Java-HIT-2019
Material for the Java 2019 courses owned and operated by HIT club
Stars: ✭ 14 (-58.82%)
Mutual labels:  hit
hit-oslab
网易云课堂李治军老师操作系统课实验报告
Stars: ✭ 34 (+0%)
Mutual labels:  hit
mcnp
📊复杂网络建模课程设计. The project of modeling of complex networks course.
Stars: ✭ 69 (+102.94%)
Mutual labels:  hit
machine learning
2018年秋季哈工大机器学习相关实验
Stars: ✭ 27 (-20.59%)
Mutual labels:  hit
groupoid.space
🧊 Інститут Формальної Математики
Stars: ✭ 35 (+2.94%)
Mutual labels:  hit
DataBase 2019 Spring
用于哈尔滨工业大学2019年春季数据库系统课程仓库
Stars: ✭ 29 (-14.71%)
Mutual labels:  hit
ngx-metrika
Angular Yandex Metrika (Модуль поддержки счетчиков Яндекс Метрика для Angular 6+)
Stars: ✭ 13 (-61.76%)
Mutual labels:  hit
2019Fall FA
2019秋季学期泛函分析笔记QAQ🦄
Stars: ✭ 43 (+26.47%)
Mutual labels:  hit
Openemr
The most popular open source electronic health records and medical practice management solution.
Stars: ✭ 1,762 (+5082.35%)
Mutual labels:  hit

Use ternary operator in Golang

查看中文文档

Golang's design philosophy is that there is only one solution to a thing, so even ternary operations are not provided. The official idea is to use if / else instead of ternary operations.

If you're used to writing code with more general statements, you might want to write code like this:

var a = 20 > 50 ? true: false;  // a = false
var b = a || 100  // b = 100
var c = b && 50  // c = 50
var fn = c > 40 && func(){ return "ok" } // fn = "ok"

After using hit, the above code can be written in this way:

var a = If(20 > 50, true, false)  // a = false
var b = Or(a, 100)  // b = 100
var c = If(b, 50)  // c = 50
var fn = If(c > 40, func(){ return "ok" }) // fn = "ok"

Beatle! Abandon dogma and enjoy hit.

Install

$ go get github.com/ymzuiku/hit

Only two API

func If(args ...interface{}) interface{}
func Or(args ...interface{}) interface{}

Examples

When need return the param, and the param is func run it and use return value

Condition implicit conversion to FALSE

  • nil
  • 0
  • error
  • ""
  • "f", "F", "false", "FALSE", "False"
  • "0", "0.0", "-0"

If(a, b, c) such as a ? b : c

if a == TRUE, return b, else return c

import . "github.com/ymzuiku/hit"

func main(){
  value1 := If(20 > 5, "ok", "cancel")
	log.Println(value1) // ok

	value2 := If("test", "ok", "cancel")
	log.Println(value2) // ok

	value3 := If("", "ok", "cancel")
	log.Println(value3) // cancel

	value4 := If("false", "ok", "cancel")
	log.Println(value4) // cancel

	value5 := If(5, "ok", "cancel")
	log.Println(value5) // ok

	value6 := If(0, "ok", "cancel")
	log.Println(value6) // cancel

	value7 := If(nil, "ok", "cancel")
	log.Println(value7) // cancel

	value8 := If(errors.New("test-error"), func() { log.Println("if err != nil, this param no use" }) // value8 is error

	// If any params is tpyeof `func()`, hit run it and return <nil>
	value9 := If(20 > 5, func() { log.Println("ok") }, func() { log.Println("cancel") }) // run: log.Println("ok"), value = nil

	// If any params is tpyeof `func()interface{}`, hit can run it and use condition.
	value10 := If(func() interface{} { return true }, func()interface{} { return "ok"  }, func()interface{} { return "cancel" })
	log.Println(value9) // ok
}

If(a, b) such as a && b

if a == TRUE, return b, else return nil

import . "github.com/ymzuiku/hit"

func main(){
  value1 := If("test", "ok")
	log.Println(value1) // "ok"

	value2 := If(500, "ok")
	log.Println(value2) // 500

	value3 := If(func() interface{} { return false }, func() { log.Println("this func no run") })
	log.Println(value3) // func no run, and value = nil

	value4 := If(func() interface{} { return true }, func() interface{} { log.Println("func is run"); return 50 })
	log.Println(value4) // func is run, and value = 50
}

Or(a, b) such as a || b

if a == TRUE, return a, else return b

import . "github.com/ymzuiku/hit"

func main(){
	value1 := Or("test", "ok")
	log.Println(value1) // "test"

	value2 := Or(500, "ok")
	log.Println(value2) // "ok"

	value3 := Or(func() interface{} { return 100 }, func() { log.Println("this func no run") })
	log.Println(value3) // func no run, and value = 100

	value4 := Or(func() interface{} { return false }, func() interface{} { log.Println("func is run"); return 50 })
	log.Println(value4) // func is run, and value = 50
}

Test list:

Pass test to 100% coverage, view test code: hit_test.go

License

MIT License

Copyright (c) 2013-present, Facebook, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].