我如何获得当前的数据库连接?
package main
import (
"github.com/labstack/echo"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
// Echo instance
e := echo.New()
db, _ := gorm.Open("mysql", "root:root@/golang")
defer db.Close()
gorm.AutoMigrate(&user.User{})
e.Logger.Fatal(e.Start(":4000"))
}
Run Code Online (Sandbox Code Playgroud)
这是我想从数据库获取用户的控制器
package controllers
import (
"github.com/labstack/echo"
)
func login(c echo.Context) error {
username := c.QueryParam("username")
}
Run Code Online (Sandbox Code Playgroud)
我如何获得 db 对象或者我需要再次打开 gorm.Open?或者为 db 对象创建单例并导入它?
我将 ExpansionPanel 与 TransitionComponent 属性一起使用,当我设置默认值(折叠)时,扩展面板工作正常,但如果值是另一个(淡入淡出、增长等),则折叠组件的高度与展开组件的高度相同
<ExpansionPanel
expanded={expanded === '1'}
onChange={handleChange('1')}
TransitionComponent={Slide}
TransitionProps={{ mountOnEnter: true }}
className={classnames('accordion-item', expanded === '1' && 'selected')}
>
Run Code Online (Sandbox Code Playgroud)
UPD:有一个片段https://codesandbox.io/s/vigorous-tree-621cz?fontsize=14&hidenavigation=1&theme=dark
我有一个整数数组:
[int1, int2, ..., intn]
Run Code Online (Sandbox Code Playgroud)
我想计算这些整数的二进制表示中有多少非零位。
例如:
bin(123) -> 0b1111011, there are 6 non-zero bits
Run Code Online (Sandbox Code Playgroud)
当然,我可以遍历整数、用途bin()和count('1')函数的列表,但我正在寻找矢量化的方法来做到这一点。
我正在尝试使用 kubectl 从远程主机连接到 microk8s 集群
kubectl config view
Run Code Online (Sandbox Code Playgroud)
结果:
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://ip:16443
name: microk8s-cluster
contexts:
- context:
cluster: microk8s-cluster
user: microk8s-admin
name: microk8s
current-context: microk8s
kind: Config
preferences: {}
users:
- name: microk8s-admin
user:
password: password
username: username
Run Code Online (Sandbox Code Playgroud)
我在服务器上使用以下命令获得了 ip、用户名、密码等凭据:
sudo microk8s.config
Run Code Online (Sandbox Code Playgroud)
结果:
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: *certificate-data*
server: https://**ip**:16443
name: microk8s-cluster
contexts:
- context:
cluster: microk8s-cluster
user: **user**
name: microk8s
current-context: microk8s
kind: Config
preferences: {}
users:
- name: admin
user: …Run Code Online (Sandbox Code Playgroud)