Giter Club home page Giter Club logo

Comments (6)

huanglao2002 avatar huanglao2002 commented on June 3, 2024 1

收到,谢谢。我自己再琢磨琢磨,有问题再请教。

from golang101.

go101 avatar go101 commented on June 3, 2024

status是一个新定义的类型,它当然和内置bool有所相同,但它不是内置的bool类型。

package main

import "fmt"

func main() {
	type status bool
	var x any = status(true)
	var y any = true // bool
	println(x == y) // false
	fmt.Printf("%T, %T\n", x, y) // main.status, bool
	
	type status2 = bool
	var z any = status2(true);
	println(z == y) // true
	fmt.Printf("%T\n", z) // bool
}

from golang101.

huanglao2002 avatar huanglao2002 commented on June 3, 2024

不知道可不可以这么理解,status只是定义的一个自定义类型。我测试了,status可以输入false、数字等数据类型。

from golang101.

go101 avatar go101 commented on June 3, 2024

status可以输入false、数字等数据类型

没太懂这句话

status 是一个新类型,而status2只是一个别名(所以它不是新类型)。

from golang101.

huanglao2002 avatar huanglao2002 commented on June 3, 2024
var x any = status(true)

//这行代码,status()里输入ture、false,数字对输出结果没任何影响。
//都是false。

from golang101.

go101 avatar go101 commented on June 3, 2024

目前Go不支持从数字类型到布尔类型的转换。所以输入数字应该编译报错。

statusbool的底层类型一致,所以它们的值可以互相转换。

from golang101.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.