Home  >  Article  >  Backend Development  >  How to convert a known type to a pointer to a type parameter in a switch?

How to convert a known type to a pointer to a type parameter in a switch?

王林
王林forward
2024-02-09 11:30:18962browse

How to convert a known type to a pointer to a type parameter in a switch?

php editor Banana will introduce to you how to convert a known type into a pointer to the type parameter in the switch. In programming, sometimes we need to convert a variable of a known type into a pointer for more flexible operations in the program. This conversion can be achieved by using the "&" operator. Prefix the variable name with the "&" operator to convert it into a pointer to the variable. In this way, we can use pointers in the program to manipulate variables instead of just assigning or reading them. Next, we will introduce in detail how to perform this type conversion and provide sample code to help everyone understand better.

Question content

I am trying to write a function that converts a byte array of a json string into another byte array based on the type parameter of the return value, The rules are as follows:

  • map[string]interface{}: converted to map[string]interface{}
  • []byte: No conversion, return as is
  • struct: Convert to struct

My code is as follows:

func getjsondata[t any](jsonbytearray []byte) (result *t, err error) {
    var buff t

    switch any(result).(type) { // https://appliedgo.com/blog/a-tip-and-a-trick-when-working-with-generics
    case *[]byte:
        result = &t(jsonbytearray)
    default:
        err = json.unmarshal(jsonbytearray, &buff)
        result = &buff
    }

    return
}

This code produces a type error when converting the type of jsonbytearray to t, as shown below:

cannot convert jsonByteArray (variable of type []byte) to type T

How to assign the pointer of this []byte type variable to the generic type return value?

Solution

Since t is constrained by any, it cannot be converted directly. You must assert &jsonbytearray is actually the same type as *t in the case of this switch:

func getjsondata[t any](jsonbytearray []byte) (result *t, err error) {
    var buff t

    switch any(result).(type) {
    case *[]byte:
        result = any(&jsonbytearray).(*t)
    default:
        err = json.unmarshal(jsonbytearray, &buff)
        result = &buff
    }

    return
}

This makes the compilation errors disappear, but it's not a particularly good design. If you need to use json.unmarshal specifically for one type (*[]byte), you're better off changing the call site instead of using a generic function.

I assume your goal is to allow the caller to get the byte slice as-is, rather than unmarshaling it. Then at the call site you can call the function as

data := GetJsonData[[]byte](jsonByteArray)

This means that at this point you already know that jsonbytearray is a byte slice.

Then, there is no reason to call this function. You can simply get the address of the parameter: data := &jsonbytearray and use json.unmarshal elsewhere.

The above is the detailed content of How to convert a known type to a pointer to a type parameter in a switch?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete