如何检查我的结构的一部分是否为0

时间:2018-06-19 11:49:38

标签: range go-templates

这是我的模板文件的一部分:

{{range .CartList}}
{{.ID}}
{{.Name}}
{{.Description}}
{{end}}

CartList是我的模板页面数据的一部分,它是[]模型。

这是我的设备结构:

type Equipment struct {
    ID int
    Name string
    Description string
    ImgPath string
    Category string
    Availability bool
    Amount string
    Storage string
    Owner int
}

基本上我想检查.ID是否为0,如果是这种情况,它应该显示一条消息,例如:“购物车中没有可用的文章”

直到现在它显示这样的空购物车[0 false 0 ..

我也尝试过:

{{if .CartList}}
{{range .CartList}}
BODY
{{else}}
"Cart not Available"
{{end}}
{{end}}

2 个答案:

答案 0 :(得分:1)

确定要检查ID是否为0吗?如果要在CartList中没有任何项目时显示替代消息,则可以使用range-else语法。

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:activiti="http://activiti.org/bpmn" 
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" 
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" 
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" 
typeLanguage="http://www.w3.org/2001/XMLSchema" 
expressionLanguage="http://www.w3.org/1999/XPath" 
targetNamespace="http://activiti.org/bpmn">
<process id="bookingProcess" name="Booking Process" isExecutable="true">
<transaction id="booking-subprocess" name="Booking">
  <parallelGateway id="parallel"></parallelGateway>
  <startEvent id="startSubprocess"></startEvent>
  <parallelGateway id="join"></parallelGateway>
  <endEvent id="endSubprocess"></endEvent>
  <userTask id="sid-93E8D137-C21F-4E91-BE48-7FA3EA8E4ED7" name="cancel flight
 "></userTask>
  <userTask id="sid-D2D0A4E3-739A-4BC7-8968-CBE42037987C" name="Cancel Hotel 
booking
"></userTask>
  <userTask id="sid-444BB983-9F53-4581-8091-DEE291E9480D" name="Book hotel
"></userTask>
  <userTask id="sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585" name="Book Flight
"></userTask>
  <subProcess id="sid-972D9FE7-C4AC-44E3-8F22-D0B575B539DC" name="subProcess" 
triggeredByEvent="true">
    <startEvent id="compensationStartEvent" name="Booking"></startEvent>
    <intermediateThrowEvent id="IntermediateThrowEvent_1" name="Flight"> 
 </intermediateThrowEvent>
    <intermediateThrowEvent id="compensateHotel" name="Hotel"> 
 </intermediateThrowEvent>
    <userTask id="updateCustomerRecord" name="Update Customer Record"> 
 </userTask>
    <endEvent id="endCompensationSubProcess"></endEvent>
    <sequenceFlow id="sid-67803F59-65CE-4891-A363-A9E866165928" 
  sourceRef="IntermediateThrowEvent_1" targetRef="compensateHotel"> 
</sequenceFlow>
    <sequenceFlow id="sid-A0749867-04BC-4536-B01C-191465A9DAC4" 
sourceRef="updateCustomerRecord" targetRef="endCompensationSubProcess"> 
  </sequenceFlow>
    <sequenceFlow id="sid-BD3ED7BA-1C65-4704-8C51-F6BD3D6EFF15" sourceRef="compensationStartEvent" targetRef="IntermediateThrowEvent_1"></sequenceFlow>
    <sequenceFlow id="sid-7CFF447D-743A-4940-9470-04380F146F35" sourceRef="compensateHotel" targetRef="updateCustomerRecord"></sequenceFlow>
  </subProcess>
  <boundaryEvent id="sid-DF01FA78-77F9-4E1A-80F5-43E9E574834A" attachedToRef="sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585" cancelActivity="false">
    <compensateEventDefinition></compensateEventDefinition>
  </boundaryEvent>
  <boundaryEvent id="compensateHotel" attachedToRef="sid-444BB983-9F53-4581-8091-DEE291E9480D" cancelActivity="false">
    <compensateEventDefinition></compensateEventDefinition>
  </boundaryEvent>
  <sequenceFlow id="SequenceFlow_2" sourceRef="startSubprocess" targetRef="parallel"></sequenceFlow>
  <sequenceFlow id="SequenceFlow_16" sourceRef="join" targetRef="endSubprocess"></sequenceFlow>
  <sequenceFlow id="SequenceFlow_4" sourceRef="parallel" targetRef="sid-444BB983-9F53-4581-8091-DEE291E9480D"></sequenceFlow>
  <sequenceFlow id="SequenceFlow_6" sourceRef="sid-444BB983-9F53-4581-8091-DEE291E9480D" targetRef="join"></sequenceFlow>
  <sequenceFlow id="SequenceFlow_3" sourceRef="parallel" targetRef="sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585"></sequenceFlow>
  <sequenceFlow id="SequenceFlow_5" sourceRef="sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585" targetRef="join"></sequenceFlow>
</transaction>
<exclusiveGateway id="ExclusiveGateway_1" default="SequenceFlow_0bulvjz"></exclusiveGateway>
<endEvent id="throwCompensation"></endEvent>
<endEvent id="EndEvent_1o7htk4"></endEvent>
<startEvent id="StartEvent_1d9syh8" activiti:formKey="sd"></startEvent>
<sequenceFlow id="notValid" name="not valid" sourceRef="ExclusiveGateway_1" targetRef="throwCompensation">
  <conditionExpression xsi:type="tFormalExpression"><![CDATA[${valid == false}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="SequenceFlow_0bulvjz" sourceRef="ExclusiveGateway_1" targetRef="EndEvent_1o7htk4"></sequenceFlow>
<sequenceFlow id="sid-0903C103-FB05-4155-9629-155571EB2EBD" sourceRef="StartEvent_1d9syh8" targetRef="booking-subprocess"></sequenceFlow>
<sequenceFlow id="sid-6B01B1FD-A748-485F-B8D2-F7BD814BE40D" sourceRef="booking-subprocess" targetRef="ExclusiveGateway_1"></sequenceFlow>
<association id="sid-F270BB03-4FAD-478A-9055-8E97E85473A2" sourceRef="compensateHotel" targetRef="sid-D2D0A4E3-739A-4BC7-8968-CBE42037987C" associationDirection="None"></association>
<association id="sid-93603CE0-EEA1-4CF6-AD7B-929A5CCF0FA7" sourceRef="sid-DF01FA78-77F9-4E1A-80F5-43E9E574834A" targetRef="sid-93E8D137-C21F-4E91-BE48-7FA3EA8E4ED7" associationDirection="None"></association>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_bookingProcess">
<bpmndi:BPMNPlane bpmnElement="bookingProcess" id="BPMNPlane_bookingProcess">
  <bpmndi:BPMNShape bpmnElement="booking-subprocess" id="BPMNShape_booking-subprocess">
    <omgdc:Bounds height="489.0" width="449.0" x="150.0" y="60.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="parallel" id="BPMNShape_parallel">
    <omgdc:Bounds height="40.0" width="40.0" x="234.0" y="216.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="startSubprocess" id="BPMNShape_startSubprocess">
    <omgdc:Bounds height="30.0" width="30.0" x="164.0" y="223.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="join" id="BPMNShape_join">
    <omgdc:Bounds height="40.0" width="40.0" x="510.0" y="218.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="endSubprocess" id="BPMNShape_endSubprocess">
    <omgdc:Bounds height="28.0" width="28.0" x="570.0" y="224.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-93E8D137-C21F-4E91-BE48-7FA3EA8E4ED7" id="BPMNShape_sid-93E8D137-C21F-4E91-BE48-7FA3EA8E4ED7">
    <omgdc:Bounds height="43.0" width="50.0" x="375.0" y="214.5"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-D2D0A4E3-739A-4BC7-8968-CBE42037987C" id="BPMNShape_sid-D2D0A4E3-739A-4BC7-8968-CBE42037987C">
    <omgdc:Bounds height="40.0" width="50.0" x="390.0" y="330.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-444BB983-9F53-4581-8091-DEE291E9480D" id="BPMNShape_sid-444BB983-9F53-4581-8091-DEE291E9480D">
    <omgdc:Bounds height="49.0" width="50.0" x="300.0" y="285.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585" id="BPMNShape_sid-02B302E9-A37B-4BD3-BE5F-24CB3C3AB585">
    <omgdc:Bounds height="59.0" width="57.0" x="310.0" y="120.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-972D9FE7-C4AC-44E3-8F22-D0B575B539DC" id="BPMNShape_sid-972D9FE7-C4AC-44E3-8F22-D0B575B539DC">
    <omgdc:Bounds height="132.0" width="390.0" x="181.0" y="390.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="compensationStartEvent" id="BPMNShape_compensationStartEvent">
    <omgdc:Bounds height="30.0" width="30.0" x="195.0" y="444.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="IntermediateThrowEvent_1" id="BPMNShape_IntermediateThrowEvent_1">
    <omgdc:Bounds height="30.0" width="30.0" x="255.0" y="444.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="compensateHotel" id="BPMNShape_compensateHotel">
    <omgdc:Bounds height="30.0" width="30.0" x="310.63778470040194" y="319.3736033156751"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="updateCustomerRecord" id="BPMNShape_updateCustomerRecord">
    <omgdc:Bounds height="80.0" width="100.0" x="372.0" y="419.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="endCompensationSubProcess" id="BPMNShape_endCompensationSubProcess">
    <omgdc:Bounds height="28.0" width="28.0" x="525.0" y="442.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-DF01FA78-77F9-4E1A-80F5-43E9E574834A" id="BPMNShape_sid-DF01FA78-77F9-4E1A-80F5-43E9E574834A">
    <omgdc:Bounds height="30.0" width="30.0" x="322.30686851045306" y="164.92485298344639"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="ExclusiveGateway_1" id="BPMNShape_ExclusiveGateway_1">
    <omgdc:Bounds height="40.0" width="40.0" x="705.0" y="289.5"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="throwCompensation" id="BPMNShape_throwCompensation">
    <omgdc:Bounds height="28.0" width="28.0" x="885.0" y="372.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="EndEvent_1o7htk4" id="BPMNShape_EndEvent_1o7htk4">
    <omgdc:Bounds height="28.0" width="28.0" x="810.0" y="210.0"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="StartEvent_1d9syh8" id="BPMNShape_StartEvent_1d9syh8">
    <omgdc:Bounds height="30.0" width="30.0" x="30.0" y="289.5"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-F270BB03-4FAD-478A-9055-8E97E85473A2" id="BPMNShape_sid-F270BB03-4FAD-478A-9055-8E97E85473A2">
    <omgdc:Bounds height="8.567425200062189" width="48.9942826076562" x="340.58568374366206" y="336.9874798770843"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape bpmnElement="sid-93603CE0-EEA1-4CF6-AD7B-929A5CCF0FA7" id="BPMNShape_sid-93603CE0-EEA1-4CF6-AD7B-929A5CCF0FA7">
    <omgdc:Bounds height="24.075708338690106" width="26.917121557182952" x="348.827602309728" y="190.22944020350943"></omgdc:Bounds>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_16" id="BPMNEdge_SequenceFlow_16">
    <omgdi:waypoint x="549.5314798586012" y="237.53147985860122"></omgdi:waypoint>
    <omgdi:waypoint x="570.0004853311445" y="237.0404704006535"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-A0749867-04BC-4536-B01C-191465A9DAC4" id="BPMNEdge_sid-A0749867-04BC-4536-B01C-191465A9DAC4">
    <omgdi:waypoint x="472.0" y="459.0"></omgdi:waypoint>
    <omgdi:waypoint x="494.0" y="459.0"></omgdi:waypoint>
    <omgdi:waypoint x="525.0310077900075" y="456.93126614733285"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-7CFF447D-743A-4940-9470-04380F146F35" id="BPMNEdge_sid-7CFF447D-743A-4940-9470-04380F146F35">
    <omgdi:waypoint x="334.8130738728852" y="346.24011369578733"></omgdi:waypoint>
    <omgdi:waypoint x="391.0716516361519" y="419.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_6" id="BPMNEdge_SequenceFlow_6">
    <omgdi:waypoint x="350.0" y="312.5"></omgdi:waypoint>
    <omgdi:waypoint x="530.5" y="312.5"></omgdi:waypoint>
    <omgdi:waypoint x="530.5" y="257.5"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_4" id="BPMNEdge_SequenceFlow_4">
    <omgdi:waypoint x="254.0" y="256.0"></omgdi:waypoint>
    <omgdi:waypoint x="254.0" y="309.5"></omgdi:waypoint>
    <omgdi:waypoint x="300.0" y="309.5"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_5" id="BPMNEdge_SequenceFlow_5">
    <omgdi:waypoint x="367.0" y="150.73949595696521"></omgdi:waypoint>
    <omgdi:waypoint x="530.5" y="150.73949595696521"></omgdi:waypoint>
    <omgdi:waypoint x="530.5" y="218.5"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_2" id="BPMNEdge_SequenceFlow_2">
    <omgdi:waypoint x="193.99704047963272" y="237.70204555338478"></omgdi:waypoint>
    <omgdi:waypoint x="234.8896103896104" y="236.8896103896104"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-67803F59-65CE-4891-A363-A9E866165928" id="BPMNEdge_sid-67803F59-65CE-4891-A363-A9E866165928">
    <omgdi:waypoint x="276.1148542890339" y="445.30297269390604"></omgdi:waypoint>
    <omgdi:waypoint x="319.52293041136807" y="348.07063062176906"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_3" id="BPMNEdge_SequenceFlow_3">
    <omgdi:waypoint x="254.0" y="216.0"></omgdi:waypoint>
    <omgdi:waypoint x="254.0" y="147.0"></omgdi:waypoint>
    <omgdi:waypoint x="310.0" y="147.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-0903C103-FB05-4155-9629-155571EB2EBD" id="BPMNEdge_sid-0903C103-FB05-4155-9629-155571EB2EBD">
    <omgdi:waypoint x="60.0" y="304.5"></omgdi:waypoint>
    <omgdi:waypoint x="150.0" y="304.50000000000006"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-93603CE0-EEA1-4CF6-AD7B-929A5CCF0FA7" id="BPMNEdge_sid-93603CE0-EEA1-4CF6-AD7B-929A5CCF0FA7">
    <omgdi:waypoint x="348.487145878088" y="189.92492288233962"></omgdi:waypoint>
    <omgdi:waypoint x="375.96257167854856" y="214.5"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-6B01B1FD-A748-485F-B8D2-F7BD814BE40D" id="BPMNEdge_sid-6B01B1FD-A748-485F-B8D2-F7BD814BE40D">
    <omgdi:waypoint x="599.0" y="304.5"></omgdi:waypoint>
    <omgdi:waypoint x="652.0" y="304.5"></omgdi:waypoint>
    <omgdi:waypoint x="705.9620253164557" y="308.5379746835443"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-BD3ED7BA-1C65-4704-8C51-F6BD3D6EFF15" id="BPMNEdge_sid-BD3ED7BA-1C65-4704-8C51-F6BD3D6EFF15">
    <omgdi:waypoint x="225.0" y="459.0"></omgdi:waypoint>
    <omgdi:waypoint x="255.0" y="459.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="notValid" id="BPMNEdge_notValid">
    <omgdi:waypoint x="725.0" y="329.5"></omgdi:waypoint>
    <omgdi:waypoint x="725.0" y="386.0"></omgdi:waypoint>
    <omgdi:waypoint x="885.0" y="386.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="SequenceFlow_0bulvjz" id="BPMNEdge_SequenceFlow_0bulvjz">
    <omgdi:waypoint x="725.0" y="289.5"></omgdi:waypoint>
    <omgdi:waypoint x="725.0" y="224.0"></omgdi:waypoint>
    <omgdi:waypoint x="810.0" y="224.0"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
  <bpmndi:BPMNEdge bpmnElement="sid-F270BB03-4FAD-478A-9055-8E97E85473A2" 
   id="BPMNEdge_sid-F270BB03-4FAD-478A-9055-8E97E85473A2">
    <omgdi:waypoint x="340.41357824087095" y="336.95738450792686"> 
  </omgdi:waypoint>
    <omgdi:waypoint x="390.0" y="345.6283546038067"></omgdi:waypoint>
  </bpmndi:BPMNEdge>
   </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
    </definitions>

引用documentation for text/template

  

如果管道的值的长度为零,则点不受影响,并且   执行T0;否则,将点设置为连续元素   数组,切片或映射的总和,然后执行T1。

您无需将其放置在if中(就像在OP中一样)。如果将{{range .CartList}} {{.ID}} {{.Name}} {{.Description}} {{else}} No Articles in your Cart available {{end}} 行之一移至{{end}}上方,则在OP的最后一个示例中所做的操作将起作用。


如果要显示ID为0的消息:

{{else}}

答案 1 :(得分:0)

{{if .CartList}} Body {{end}}是您要寻找的