首页 > 其他分享 >Go - Creating One - Time Structs

Go - Creating One - Time Structs

时间:2023-10-07 15:36:27浏览次数:37  
标签:struct person Person Go div data Structs string Creating

 

person   :=   struct   { 
      Id      int 
      Name    string 
      Email   string 
}{ 1 ,   "Chang  Sau  Sheong" ,   "[email protected]" }

 

person   =   struct   { 
      Id      int 
      Name    string 
      Email   string 
}{ 
      Id :      1 , 
      Name :    "Chang  Sau  Sheong" , 
      Email :   "[email protected]" , 
}

 

There are a few use cases for anonymous structs but one particular case is pretty useful for bundling multiple pieces of unrelated data together to be sent to a function. Here’s a concrete example.

Say you want to send a struct to an HTML template such that an HTML page will be generated by substituting data from the struct into various locations in the template:

type   Person   struct   { 
      Id      int 
      Name    string 
      Email   string 
} 
person   :=   Person { 1 ,   "Chang  Sau  Sheong" ,   "[email protected]" } 
tpl   :=   `<div> 
    <div>{{  .Id  }}</div> 
    <div>{{  .Name  }}</div> 
    <div>{{  .Email  }}</div> 
</div> 
`
templ   :=   template . Must ( template . New ( "person" ). Parse ( tpl )) 
templ . Execute ( os . Stdout ,   person )

You have a template named tpl and want to substitute various pieces of data in it from a struct you pass to it. In this case, you pass the Person struct to the Execute method. The template will substitute the data at the various locations and if you run the preceding code, you will get this:

< div > 
 	 < div > 1 </ div > 
 	 < div > Chang  Sau  Sheong </ div > 
 	 < div > [email protected] </ div > 
</ div >

This is all well and good but what if you want to display a piece of information on the same page that has nothing to do with a person, for example, a message for the user? You can’t add a field into the Person struct, as that would be silly and awkward. You could define another struct that wraps around the message and the Person struct but it’s only going to be used for that one single page. You have many other pages with other things to show besides the main piece of data. You can’t possibly define multiple structs, each to pass in some data to the page.

Here is where anonymous structs make a lot of sense:

type   Person   struct   { 
      Id      int 
      Name    string 
      Email   string 
} 
person   :=   Person { 1 ,   "Chang  Sau  Sheong" ,   "[email protected]" } 
tpl   :=   `<h1>{{  .Message  }}</h1> 
<div> 
    <div>{{  .P.Id  }}</div> 
    <div>{{  .P.Name  }}</div> 
    <div>{{  .P.Email  }}</div> 
</div> 
` 
data   :=   struct   { 
      P         Person 
      Message   string 
}{ person ,   "Hello  World!" } 

templ   :=   template . Must ( template . New ( "person" ). Parse ( tpl )) 
templ . Execute ( os . Stdout ,   data )

Instead of sending in the Person struct, you create an anonymous struct that wraps around Person as well as a string message and send that in instead. 

This way, you can preserve your defined structs and, if there are other pieces of data to send to the page, you can create an anonymous struct to wrap around the data and send that in.

 

标签:struct,person,Person,Go,div,data,Structs,string,Creating
From: https://www.cnblogs.com/zhangzhihui/p/17746409.html

相关文章

  • 2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点, 坐标为double类型,精度最多
    2023-10-07:用go语言,给定n个二维坐标,表示在二维平面的n个点,坐标为double类型,精度最多小数点后两位,希望在二维平面上画一个圆,圈住其中的k个点,其他的n-k个点都要在圆外。返回一个圆心和半径,表示哪个圆可以圈住其中的k个点。坐标和半径都是double类型,最多保留小数点后两位。下面......
  • 代码源:a-good string(CF1385D,分支)
    传送点击查看代码#include<bits/stdc++.h>usingnamespacestd;chars[131080];int_solve(intL,intR,charx){ if(L==R)returns[L]!=x; intM=L+(R-L)/2; intt1=0,t2=0; for(inti=L;i<=M;++i)if(s[i]!=x)t1++; for(inti=M+1;i<=R;++i)if(s[i]!=x)......
  • AIGC革新,将文字或者LOGO融入AI视频基于PIKA-labs(Python3.10)
    很多平台都会禁止用户使用带有网址或者二维码的头像以及文章配图,这样可以有效的防止用户的一些“导流”行为。当然,头像、文章或者视频现在都是AI来审,毕竟现在人工的成本实在太高,但是如果我们把文字元素直接融入图像或者视频之中,如此一来,AI也会很难识别出一些“导流”的元素。本次......
  • vulnhub - Aragog - writeup
    信息收集目标开放了80、22端口。root@Locklytemp/tmp»arp-scan-Ieth1-lInterface:eth1,type:EN10MB,MAC:00:0c:29:fa:3d:23,IPv4:192.168.56.106Startingarp-scan1.10.0with256hosts(https://githu......
  • Go - Creating Struct Instances
    Problem: Youwanttocreateaninstanceofastruct.Solution: Createastructinstancedirectlyusingthenameofthestruct,orapointertoastructinstanceusingthenewkeyword. Therearetwowaystocreateaninstanceofastruct.Thefirstist......
  • Strimzi Kafka Bridge(桥接)实战之三:自制sdk(golang版本)
    欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos本篇概览本文是《StrimziKafkaBridge(桥接)实战》的第三篇,前文咱们掌握了StrimziKafkaBridge的基本功能:基于http提供各种kafka消息的服务此刻,如果想通过http接口调......
  • WIN11 安装 SQL Server 2019,SQLSERVER2022, MYSQL 8.0 ,Docker,Mongodb失败故障分析
    最近研究数据库性能调优遇到各种数据库各种装不上,不知道熬了多少根软白沙,熬了多少颗张三疯,问了多少AI,查了多少网页,熬了两天,终于搞明白了一件事:那就是WIN11ONARM(因为拿的是MACPROM2做.NET平台开发安装)SQLSERVER2019,SQLSERVER2022,MYSQL8.0,Docker,Mongodb失败故障分析,最终极......
  • 基于goravel的CMS,企业官网通用golang后台管理系统
    2023年9月11日10:47:00仓库地址:https://gitee.com/open-php/zx-goravel-websiteCMS,企业官网通用PHP后台管理系统框架介绍GoravelSCUI后端开发组件go1.20Goravel1.13数据库sql(使用最新日期文件)goravel\doc\sql_bakmysql8.0前端开发组件scui1.6.9nodev14.21.......
  • 超能组合:python 的开发效率 + go 的并发 + shell 的短小精悍
    工具思维:利用合适的工具做合适的事情,然后合理地加以组合。在”谈谈程序员应当具备的技术思维“一文中谈到了工具思维。本文对工具思维作一发挥运用。批量下载图片程序员总是有点”美图“爱好的。由于程序员通常又是比较”懒惰“的(可没有那个耐心和体力去一页页点开再点击按......
  • Go - Parsing Time Displays Into Structs
     funcmain(){str:="4:31am+0800onOct1,2021"layout:="3:04pm-0700onJan2,2006"t,err:=time.Parse(layout,str)iferr!=nil{log.Println("C......