简体   繁体   中英

golang type conversion after type assertion

Take these two bits of code: 1. http://play.golang.org/p/Oh6xNm2dRK 2. http://play.golang.org/p/Vd-6AGCBKQ

In 1. the media is first created as the empty interface and then type asserted to a string. In 2. the media is a string.

Both try to convert the media to a byte array, why the difference? Aren't they both strings by that time?

The first one doesn't change the type of media , which is already defined as interface{} . You need to set it to a new string variable, or do it inline:

http://play.golang.org/p/QB3ey_e3io

http://play.golang.org/p/RJqBJ4telB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM