Introduction
The GoFrame
framework provides a very powerful and easy-to-use type conversion package gconv
, which can convert common data types into specified data types, seamlessly converting between common basic data types, and also supports conversion of any type to a struct
object. Since the gconv
module internally prioritizes the use of assertions over reflection, the execution efficiency is very high.
Notes:
The primary goal of the gconv
package is to provide simple and efficient type conversion functionality. Developers should be aware of the input parameters for the conversion and the current business scenario being used. If some methods fail to convert, the method will not return the reason for the error, nor will it cause a panic
, but will directly return the value after the conversion fails. Therefore, developers often need to comprehensively judge the correctness of the result based on the return value and the current business scenario.
Usage:
import "github.com/gogf/gf/v2/util/gconv"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/util/gconv
Method List:
The method list may lag behind the code, please refer to the API documentation for details.
Documents
📄️ Type Conversion - Types
The method of common basic type conversion using the GoFrame framework. Focuses on the application of the gconv package in converting integers, floating-point numbers, booleans, strings, etc. Provides simple and understandable code examples demonstrating how to use these conversion functions for efficient type conversion in actual development.
📄️ Type Conversion - Map
Use the gconv.Map method in the GoFrame framework for type conversion, including converting any map or struct/*struct type to a map[string]interface{} type. It supports property tags and custom tags, and can achieve recursive conversion through the MapDeep method to parse out the detailed structure of nested objects, suitable for multi-layer data processing.
📄️ Type Conversion - Struct
Using the gconv module of GoFrame for struct conversion, including mapping from various data types to structs, automatic object creation, recursive conversion, and mapping rules, among other practical features, to help developers enhance coding efficiency and project maintenance capabilities.
📄️ Type Conversion - Structs
Type conversion methods in the GoFrame framework, focusing on using the Structs method for the conversion of struct arrays. The Structs method is similar to the Struct method and extends its support to struct arrays. The article also provides specific code examples demonstrating how to apply this feature in practice.
📄️ Type Conversion - Scan
Learn how to use the powerful Scan method in the GoFrame framework to automatically convert arbitrary parameters into structs, struct arrays, maps, and map arrays. This article introduces the definition of the Scan method and its application in the Go language, providing multiple programming examples to help quickly understand and master this function, offering developers an efficient and convenient type conversion solution.
📄️ Type Conversion - Converter
From version v2.6.2 of the GoFrame framework, the conversion component adds the Converter feature, allowing developers to customize type conversion logic. This document showcases how to use the Converter feature for registering and applying custom conversion methods for data structures and alias types, as well as how to handle these types of conversions with the help of gconv.Scan and gconv.ConvertWithRefer methods.
📄️ Type Conversion - Interface
Use the gconv module in the GoFrame framework for type conversions, especially custom conversions through the UnmarshalValue interface. A detailed discussion on the application of reflection features in complex type conversions and their performance impact, along with multiple code examples, including custom struct conversion and TCP data unpacking, to help developers optimize conversion efficiency.
📄️ Type Conversion - Performance
A detailed introduction and analysis of the basic type conversion usage in the GoFrame framework through performance benchmark tests. By performing performance tests on multiple different data type conversions, it demonstrates their efficiency under different conditions, providing developers with optimization references and guidance when using the GoFrame framework.