将方法或函数的指针设置为nil

时间:2019-02-16 19:37:37

标签: go

为什么这两个# the SOURCE definiton lets move your makefile to another position CONFIG = LIBRARY # set pathes to the correct directories SRC_DIR = ../../../../source/Lib/TLibCommon INC_DIR = ../../../../source/Lib LIB_DIR = ../../../../lib BIN_DIR = ../../../../bin SRC_DIR1 = ../../../../source/Lib/libmd5 SRC_DIR2 = SRC_DIR3 = SRC_DIR4 = USER_INC_DIRS = -I$(SRC_DIR) USER_LIB_DIRS = ifeq ($(HIGHBITDEPTH), 1) HBD=HighBitDepth else HBD= endif # intermediate directory for object files OBJ_DIR = ./objects$(HBD) # the library name PRJ_NAME = TLibCommon$(HBD) # version information MAJOR_VER = 0 MINOR_VER = 1 VER = $(MAJOR_VER).$(MINOR_VER) # defines to set DEFS = -DMSYS_LINUX # set objects OBJS = \ $(OBJ_DIR)/Debug.o \ $(OBJ_DIR)/TComPicYuv.o \ $(OBJ_DIR)/TComYuv.o \ $(OBJ_DIR)/ContextModel.o \ $(OBJ_DIR)/ContextModel3DBuffer.o \ $(OBJ_DIR)/SEI.o \ $(OBJ_DIR)/TComCABACTables.o \ $(OBJ_DIR)/TComSampleAdaptiveOffset.o \ $(OBJ_DIR)/TComBitStream.o \ $(OBJ_DIR)/TComChromaFormat.o \ $(OBJ_DIR)/TComDataCU.o \ $(OBJ_DIR)/TComLoopFilter.o \ $(OBJ_DIR)/TComMotionInfo.o \ $(OBJ_DIR)/TComPattern.o \ $(OBJ_DIR)/TComPic.o \ $(OBJ_DIR)/TComPicSym.o \ $(OBJ_DIR)/TComPicYuvMD5.o \ $(OBJ_DIR)/TComPrediction.o \ $(OBJ_DIR)/TComRdCost.o \ $(OBJ_DIR)/TComRom.o \ $(OBJ_DIR)/TComSlice.o \ $(OBJ_DIR)/TComTrQuant.o \ $(OBJ_DIR)/TComTU.o \ $(OBJ_DIR)/TComInterpolationFilter.o \ $(OBJ_DIR)/libmd5.o \ $(OBJ_DIR)/TComWeightPrediction.o \ $(OBJ_DIR)/TComRdCostWeightPrediction.o \ LIBS = -lpthread DEBUG_LIBS = RELEASE_LIBS = STAT_LIBS = DYN_LIBS = -ldl # the libraries to link with STAT_DEBUG_LIBS = STAT_RELEASE_LIBS = DYN_DEBUG_LIBS = DYN_RELEASE_LIBS = # name of the base makefile MAKE_FILE_NAME = ../../common/makefile.base # include the base makefile include $(MAKE_FILE_NAME) 函数都不会将指针更改为nil,我该如何创建该函数?

destroy

输出:

package main

import (
    "fmt"
)

type position struct {
    x int
    y int
}

func (p *position) destroy() {
    p = nil
}

func destroy(p *position) {
    p = nil
}

func main() {
    p1 := &position{1,1}
    p2 := &position{2,2}
    p1.destroy()
    destroy(p2)

    if p1 == nil {
        fmt.Println("p1 == nil")
    } else {
        fmt.Println(p1)
    }

    if p2 == nil {
        fmt.Println("p2 == nil")
    } else {
        fmt.Println(p2)
    }

}

https://play.golang.org/p/BmZjX1Hw24u

1 个答案:

答案 0 :(得分:4)

您需要一个pointer to pointer才能更改指针的值。

这是您的代码示例,经过修改以执行此操作(playground):

package main

import (
    "fmt"
)

type position struct {
    x int
    y int
}

func destroy(p **position) {
    *p = nil
}

func main() {
    p1 := &position{1, 1}
    destroy(&p1)

    if p1 == nil {
        fmt.Println("p1 == nil")
    } else {
        fmt.Println(p1)
    }
}

使用您当前的代码

func destroy(p *position) {
    p = nil
}

destroy内,p是一个保存position结构的地址的值。通过为p本身分配内容,您只需使其包含其他position结构(或nil)的地址即可。您没有修改传入的原始指针。

这与试图通过为其分配参数来修改其参数的函数没有什么不同:

// This will not actually modify the argument passed in by the caller
func setto2(value int) {
  value = 2
}

go规范在section about calls and call parameters中说:

  

求值后,调用的参数将传递给   函数的值,被调用函数开始执行。的   函数的返回参数按值传递回   函数返回时调用函数。