小编ove*_*nge的帖子

如何删除 if 块中的否定?

遵循此处推荐的 do..while 模式:

for {
    work()
    if !condition {
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的代码使用 for 循环实现 do..while(condition) :

var r *s
var err error
counter := 0

for { // do..while(specificerror && < MAX && trigger_not_initiated)

    r, err = f()
    counter = counter + 1
    if !(err != nil &&
        strings.Contains(err.Error(), "specificerror") &&
        counter < MAX &&
        !IsTriggerInitiated()) {
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

if但审查小组建议通过删除语句中的否定(条件)中的否定来使 if 条件更具可读性

如何删除否定(条件)forif语句中的否定?

boolean-logic go boolean-algebra

-2
推荐指数
1
解决办法
238
查看次数

浮动和位置如何协同工作?

在下面的代码中,

<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>
Run Code Online (Sandbox Code Playgroud)
.box { 
   display: inline-block; 
   background: red; 
   width: 100px; 
   height: 100px; 
   float: left; 
   color: black; 
}

#one{
    background: red;
}
#two { 
   position: absolute;
   background: yellow; 
}
#three{
    background: green;
}
#four{
    background: blue;
}
Run Code Online (Sandbox Code Playgroud)

-

盒子"两个"绝对定位并且远离文件的流动,盒子"三"和"四"代替盒子"两个",由此,盒子"两个"被移位作为最后一个元件,如图所示下面,看起来不错,

在此输入图像描述

但是在下面的代码中,

<div id="parent-div">
      <div id="default">Default</div>
      <div id="centered">Centered</div>
      <div id="centered-text">Centered Text</div>
    </div>

   <div id="top-left-pos">Top Left

    </div>

    <div id="another-pos">Another pos
    </div>
Run Code Online (Sandbox Code Playgroud)
#parent-div{
    background: #B3bEb5;
    border: 0.1em solid black;
}

#default{
  background: #DBE9F4;
} …
Run Code Online (Sandbox Code Playgroud)

html css

-3
推荐指数
1
解决办法
289
查看次数

相互排斥 - 如何同步工作?

回答中所述,synchronized使用compareAndSwap实现,这是非阻塞算法.

  1. synchronized不使用的情况下wait(),线程状态是否设置为BLOCKED

  2. 线程处于BLOCKED&WAITING状态是否消耗CPU周期?

java multithreading synchronized

-3
推荐指数
1
解决办法
165
查看次数

这是基于消息总线的发布-订阅模式吗?

根据这个答案

消息总线是一种消息传递基础结构,允许不同的系统通过一组共享的接口(消息总线)进行通信。

https://i.stack.imgur.com/5PkJy.gif


以下是创建消息中心以将发布者与多个订阅者进行通信所启动的createHub()功能和Run()方法main()

type PubHub struct {
    subscribers map[*subscriptionmediator.HandlerSubscription]struct{}
    Register    chan *subscriptionmediator.HandlerSubscription
    Unregister  chan *subscriptionmediator.HandlerSubscription
    Broadcast   chan *events.Env
}


func createHub() *PubHub {

    return &PubHub{
        subscribers: map[*subscriptionmediator.HandlerSubscription]struct{}{},
        Register:    make(chan *subscriptionmediator.HandlerSubscription),
        Unregister:  make(chan *subscriptionmediator.HandlerSubscription),
        Broadcast:   make(chan *events.Envelope),
    }
}

func (h *PubHub) Run() {

    for {
        select {
        case subscriber := <-h.Register:
            h.subscribers[subscriber] = struct{}{}

        case subscriber := <-h.Unregister:
            if _, ok := h.subscribers[subscriber]; ok {
                delete(h.subscribers, subscriber)
            }
        case message := <-h.Broadcast: …
Run Code Online (Sandbox Code Playgroud)

design-patterns go publish-subscribe

-3
推荐指数
1
解决办法
283
查看次数

如何将数据从一种类型复制到具有相同结构的另一种类型?

在下面的代码中:

type ProductEntity struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type ProductEntityList []*ProductEntity

type PostRequestModel struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type RequestBody []*PostRequestModel

func convertModelToEntity(modelList RequestBody) ProductEntityList {

    // return entity.ProductEntityList(modelList) // type conversion error 

}
Run Code Online (Sandbox Code Playgroud)

如何将数据从一种类型复制到另一种具有相同结构的类型?因为RequestBody …

struct pointers go slice

-3
推荐指数
1
解决办法
54
查看次数

Cloudfoundry密码-代码漏洞

以下是客户端库用于连接Cloud Foundry 的GO代码。

c := &cfclient.Config{
    ApiAddress: "https://x.y.z.cloud",
    Username:     "admin",
    Password:     "admin",
}

client, _ := cfclient.NewClient(c)
Run Code Online (Sandbox Code Playgroud)

由于密码易读,该源代码容易受到攻击,进入源代码管理。

目前,使用上述代码的应用程序正在Cloud Foundry(PAAS)外部运行。

AWS云(IAAS)引入了称为角色的概念,该角色无需凭证即可进行访问。


避免在源代码中显示可见密码的最佳实践是什么?CredHub凭据配置是否可以帮助客户端库以安全方式连接?

go paas cloud-foundry amazon-iam pivotal-cloud-foundry

-4
推荐指数
1
解决办法
74
查看次数

go test 错误 - 没有测试文件 - GOPATH 中的错误

在以下环境中:

code$ pwd
/home/user1/code
code$ echo $GOPATH
/home/user1/golib:/home/user1/code
code$ ls
bin  pkg  src 
code$ ls src/github.com/myhub/codingtest/
main.go  test_main.go
code$ 
code$ 
code$ 
code$ 
code$ 
code$ cat src/github.com/myhub/codingtest/test_main.go 
package main

import "testing"

func TestSplit(t *testing.T) {
        gotAllButLast, gotLast := split(2013)
        wantAllButLast := 201
        wantLast := 3
        if gotAllButLast != wantAllButLast {
                t.Errorf("got %d but expected %d", gotAllButLast, wantAllButLast)
        }
        if wantLast != gotLast {
                t.Errorf("got %d but expected %d", gotLast, wantLast)
        }
}
code$ 
code$ 
code$ 
code$ 
code$ 
code$ cat src/github.com/myhub/codingtest/main.go 
package …
Run Code Online (Sandbox Code Playgroud)

go

-4
推荐指数
1
解决办法
1335
查看次数

上下文 - WithDeadline() 与 WithTimeout()

下面的代码:

// Sample program to show how to use the WithDeadline function
// of the Context package.
package main

import (
    "context"
    "fmt"
    "time"
)

type data struct {
    UserID string
}

func main() {

    // Set a duration.
    // duration := 150 * time.Millisecond
    duration := time.Now().Add(3 * time.Second)

    // Create a context that is both manually cancellable and will signal
    // a cancel at the specified duration.
    ctx, cancel := context.WithDeadline(context.Background(), duration)
    defer cancel()

    // Create a channel …
Run Code Online (Sandbox Code Playgroud)

channel go goroutine

-4
推荐指数
2
解决办法
1356
查看次数

atomic.AddInt64() 是否强制从/向主内存获取/更新值?

在下面的代码中使用atomic.AddInt64

func main() {

    // Number of goroutines to use.
    const grs = 2

    // wg is used to manage concurrency.
    var wg sync.WaitGroup
    wg.Add(grs)

    // Create two goroutines.
    for g := 0; g < grs; g++ {
        go func() {
            for i := 0; i < 2; i++ {
                atomic.AddInt64(&counter, 1)
            }

            wg.Done()
        }()
    }

    // Wait for the goroutines to finish.
    wg.Wait()

    // Display the final value.
    fmt.Println("Final Counter:", counter)
}
Run Code Online (Sandbox Code Playgroud)

或在下面的代码中使用atomic.LoadInt64

func writer(i …
Run Code Online (Sandbox Code Playgroud)

go goroutine

-4
推荐指数
1
解决办法
44
查看次数

该类型别名语法是否为Go?

我没有在中得到以下语法../go/src/net/http/server.go

var defaultServeMux ServeMux
Run Code Online (Sandbox Code Playgroud)

哪里

ServeMux 是一个结构

type ServeMux struct {
    mu    sync.RWMutex
    m    map[string]muxEntry
    es    []muxEntry
    hosts    bool
}
Run Code Online (Sandbox Code Playgroud)

在GO中,类型别名看起来像type T1 = T2

上面的语法(用于defaultServeMux)与类型别名有关吗?

go

-5
推荐指数
1
解决办法
53
查看次数

类型转换规则 - 将类型转换为其基础类型

在将一个对象(type1)成员复制到另一个对象(type2)成员的过程中进入以下场景:

package main

import "fmt"

type SomeType string

func main() {
    source := SomeType("abc")
    dest := string(source) // this works 
    fmt.Println(dest)
}
Run Code Online (Sandbox Code Playgroud)

对于这种类型转换(string(source)),应用了 Go 规范中的哪个规则,以转换为基础类型?

go

-5
推荐指数
1
解决办法
123
查看次数

'Example.class'指向的对象什么时候出现?

以下是该计划:

package annotationtype;

public class Example {

    public static void main(String[] args){

    }
}
Run Code Online (Sandbox Code Playgroud)

用下面的字节代码编译.

Classfile /D:/git/Java_programming/JavaCode/bin/annotationtype/Example.class
  ......
  Compiled from "Example.java"
public class annotationtype.Example
  .......
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Class              #2             // annotationtype/Example
   #2 = Utf8               annotationtype/Example
   #3 = Class              #4             // java/lang/Object
   ......
  #18 = Utf8               SourceFile
  #19 = Utf8               Example.java
{
  public annotationtype.Example();
  ........
  public static void main(java.lang.String[]);
  ......
}
SourceFile: "Example.java"
Run Code Online (Sandbox Code Playgroud)

使用eclipse编辑器,在main()方法中,如果我输入,

Example.,eclipse编辑器立即提供class类型的成员Class<annotationtype.Example>

我的理解是,

低于字节码,

#1 = Class …
Run Code Online (Sandbox Code Playgroud)

java eclipse

-31
推荐指数
1
解决办法
449
查看次数