dongyue110702
2018-07-10 18:59
浏览 400
已采纳

golang错误:“未定义(类型字符串没有字段或方法)”

i use this library golang.org/x/crypto/bcrypt to hash password and compare hash with password but I having problem see below:

main.go file

package main

import (
        "./hash"
)

func main() {
        password := "passwd"
        hash := "hhhhhhhhaaaaaaaaaassssssssssshhhhhhhhhhh"

        check := hash.CheckPasswordHash(password, hash)
}

hash/hash.go file

package hash

import "golang.org/x/crypto/bcrypt"

func HashPassword(password string) (string, error) {
        bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)

        return string(bytes), err
}

func CheckPasswordHash(password, hash string) bool {
        err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))

        return err == nil
}

if you run this:

$ go run main.go

It will show this error:

./main.go:11:15: hash.CheckPasswordHash undefined (type string has no field or method CheckPasswordHash)

why this error?

图片转代码服务由CSDN问答提供 功能建议

我使用此库 golang.org/x/crypto/bcrypt 对密码进行哈希处理并将哈希与密码进行比较,但是我在下面遇到问题:</ p>

main.go文件< / p>

 包main 
 
import(
“ ./hash"
)

func main(){
 password:=” passwd“ 
 hash:  =“ hhhhhhhhaaaaaaaaaassssssssssshhhhhhhhhhhh” 
 
检查:= hash.CheckPasswordHash(密码,哈希)
} 
 </ code> </ pre> 
 
 

hash / hash.go文件</ p> \ n

 程序包哈希
 
导入“ golang.org/x/crypto/bcrypt”

func HashPassword(密码字符串)(字符串,错误){
字节,错误:= bcrypt。  GenerateFromPassword([] byte(password),14)
 
返回字符串(字节),err 
} 
 
func CheckPasswordHash(密码,哈希字符串)bool {
 err:= bcrypt.CompareHashAndPassword([] byte  (hash),[] byte(password))
 
 return err == nil 
} 
 </ code> </ pre> 
 
 

,如果运行此命令:</ p> \ ñ

   $ go run main.go 
 </ code> </ pre> 
 
 

它将显示此错误:</ p>

./ main.go:11:15:hash.CheckPasswordHash未定义(类型字符串没有 字段或方法CheckPasswordHash)</ p> </ blockquote>

为什么会出现此错误?</ p> \ n </ div>

2条回答 默认 最新

相关推荐 更多相似问题