Git Product home page Git Product logo

Comments (4)

chai2010 avatar chai2010 commented on May 4, 2024 1

可能是故意的,data 有点类型 interface 中 type 指针的味道,用来区分是否是真的 nil

from advanced-go-programming-book.

chai2010 avatar chai2010 commented on May 4, 2024

是想表达以下2种写法是等价的:

	var a []int = nil
	var b = []int{}

from advanced-go-programming-book.

chai2010 avatar chai2010 commented on May 4, 2024

两个确实是不一样的:

package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main() {
	var a = []int{}
	var b []int = nil

	pa := (*reflect.SliceHeader)(unsafe.Pointer(&a))
	pb := (*reflect.SliceHeader)(unsafe.Pointer(&b))

	fmt.Printf("a: %#v\n", *pa)
	fmt.Printf("b: %#v\n", *pb)
}

输出:

a: reflect.SliceHeader{Data:0xc42003ff20, Len:0, Cap:0}
b: reflect.SliceHeader{Data:0x0, Len:0, Cap:0}

nil初始化的slice底层数据指针是nil

from advanced-go-programming-book.

chai2010 avatar chai2010 commented on May 4, 2024

经查看内容,发现已经修改了描述

from advanced-go-programming-book.

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.