


How to access properties of interface type parameters without using type assertions in Go language?
Golang interface type parameter attribute access: Avoid type assertions
This article introduces how to access the properties of interface type parameters in Go language without relying on type assertions. The sample code shows that doRun
function receives PhoneFoo
interface type parameters and needs to access os
attributes of IPhoneFoo
structure. Direct access to phone.os
will cause an error because the interface only defines methods and does not contain structure fields.
The original code tries to access phone.os
directly, which is wrong, because the interface only defines methods and cannot directly access fields of the underlying structure. Therefore, other methods need to be used to access os
attribute.
This paper uses a reflection mechanism to solve this problem. Reflection allows runtime checking and manipulating object type information. The improved code uses reflect
package to implement:
-
reflect.ValueOf(phone)
Gets the reflected value ofphone
interface variable. - Since
phone
is a pointer to theIPhoneFoo
structure, callElem()
method to get the value of the structure itself. -
v.Field(0)
gets the first field of the structure (indexed to 0), that is, theos
field. -
fmt.Println(os.String())
orfmt.Println(os)
prints the value of theos
field.os.Type()
prints field type information.
Through reflection, we dynamically obtain and print the os
attribute value of the IPhoneFoo
structure, avoiding the code redundancy and maintenance problems caused by type assertions. This method is suitable for handling a variety of different types of scenarios with common interfaces.
The above is the detailed content of How to access properties of interface type parameters without using type assertions in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Apple's iPhone 17 may usher in a major upgrade to cope with the impact of strong competitors such as Huawei and Xiaomi in China. According to the digital blogger @Digital Chat Station, the standard version of iPhone 17 is expected to be equipped with a high refresh rate screen for the first time, significantly improving the user experience. This move marks the fact that Apple has finally delegated high refresh rate technology to the standard version after five years. At present, the iPhone 16 is the only flagship phone with a 60Hz screen in the 6,000 yuan price range, and it seems a bit behind. Although the standard version of the iPhone 17 will have a high refresh rate screen, there are still differences compared to the Pro version, such as the bezel design still does not achieve the ultra-narrow bezel effect of the Pro version. What is more worth noting is that the iPhone 17 Pro series will adopt a brand new and more

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

There are two ways to view XML files: Android phones: use file manager or third-party applications (XML Viewer, DroidEdit). iPhone: Transfer files via iCloud Drive and use the Files app or third-party app (XML Buddha, Textastic).

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

There is no built-in XML viewer on iPhone, and you can use third-party applications to open XML files, such as XML Viewer, JSON Viewer. Method: 1. Download and install the XML viewer in the App Store; 2. Find the XML file on the iPhone; 3. Press and hold the XML file to select "Share"; 4. Select the installed XML viewer app; 5. The XML file will open in the app. Note: 1. Make sure the XML viewer is compatible with the iPhone iOS version; 2. Be careful about case sensitivity when entering file paths; 3. Be careful with XML documents containing external entities

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.
